﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.HSSF.Util;
using NPOI.POIFS.FileSystem;
using NPOI.HPSF;
using System.IO;
public partial class TLThanhToanTienMat : System.Web.UI.Page
{
    double tongtien = 0;
    int pc;
    double t;
    SqlParameter[] p;
    clsXuLy ex = new clsXuLy();
    string frmName = "frmTLThanhToanTienMat";
    static int f_SuDung = 0, f_Them = 0, f_Sua = 0, f_Xoa = 0, f_XemTatCa = 0, f_InBaoCao = 0;
    public string Header = System.Configuration.ConfigurationSettings.AppSettings["Header"].ToString();
    static DataTable dt;
    static int PageNumber = 6;
    static int CurrentPage = 1;
    double tienton;
    double max;
    string idmax;
    static DataTable dtPrint;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Convert.ToString(Session["user"])))
            gstGetMess("Hết phiên làm việc, vui lòng đăng nhập để tiếp tục", "Default.aspx");
        else
        {
            if (!IsPostBack)
            {


                string idnguoidung = Session["IDUser"].ToString();
                string sql = "select SuDung,Them,Sua,Xoa,InBaoCao,XemTatCa from XDAPhanQuyen where IDnguoidung='" + idnguoidung + "' and tag='" + frmName + "'";

                DataTable dt2 = ex.GetData_Text(sql);
                if (dt2.Rows.Count > 0)
                {
                    f_SuDung = int.Parse(dt2.Rows[0]["SuDung"].ToString());
                    f_Them = int.Parse(dt2.Rows[0]["Them"].ToString());
                    f_Sua = int.Parse(dt2.Rows[0]["Sua"].ToString());
                    f_Xoa = int.Parse(dt2.Rows[0]["Xoa"].ToString());
                    f_InBaoCao = int.Parse(dt2.Rows[0]["InBaoCao"].ToString());
                    f_XemTatCa = int.Parse(dt2.Rows[0]["XemTatCa"].ToString());
                }
                if (f_SuDung.Equals(1))
                {
                    CurrentPage = 1;
                    btnCancel_Click(sender, e);
                    Title = "Quản lý thanh toán tiền mặt | " + Header;

                    LoadNhanVien();

                    LoadNhanVien_TimKiem();
                    for (int i = 1; i <= 12; i++)
                    {
                        dlThang.Items.Add(new ListItem("Tháng " + i.ToString(), i.ToString()));

                    }
                    dlThang.Items.Insert(0, new ListItem("Theo thời gian", ""));
                    dlThang.SelectedIndex = 0;

                    int year1 = DateTime.Now.Year - 3;
                    int year2 = DateTime.Now.Year;
                    for (int i = year2; i >= year1; i--)
                    {
                        dlNam.Items.Add(new ListItem("Năm " + i.ToString(), i.ToString()));

                    }
                    dlNam.SelectedValue = year2.ToString();

                    txtTuNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
                    txtDenNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
                    dlNam.Visible = false;



                }
                else
                    gstGetMess("Bạn không có quyền xem trang này", "Home.aspx");

            }
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {

        txtngaythang.Text = "";
        txtbophan.Enabled = false;
        txtchucvu.Enabled = false;

        ckhoantheophieuchi.Checked = false;
        txtsotien.Text = "";
        txtsotienhoanlai.Text = "";
        txtsotienton.Text = "";

        txtsotienhoanlai.Visible = false;
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", 1);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;
        dt = ex.GetData("sp_xdaThietLapPhieuChi_List", p);
        GV.DataSource = dt;
        GV.DataBind();
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", 1);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;
        GVDoiTuong.DataSource = ex.GetData("sp_XDANoiDungTTTienMat_List", p);
        GVDoiTuong.DataBind();

        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", 1);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;
        GVVAT.DataSource = ex.GetData("sp_xdanoidungtttienmat_VAT", p);
        GVVAT.DataBind();
        tongtienh.Text = "";
        drphieuchiton.SelectedIndex = 0;
        drphieuchiton.Visible = false;
        drnhanvien.SelectedIndex = 0;
        txtbophan.Text = "";
        txtchucvu.Text = "";

        LoadDoiTuongChi();
        FirstGridViewRow();

    }
    //void LoadPhieuChi()
    //{
    //    drphieuchi.Items.Clear();

    //    drphieuchi.Items.Insert(0, new ListItem("--Chọn phiếu chi--", ""));
    //    drphieuchi.SelectedIndex = 0;
    //}
    protected void btnSave_Click(object sender, EventArgs e)
    {

    }
    private void gstGetMess(string gstMess, string gstLink)
    {
        if (gstLink == "")
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "')", true);
        else
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "');window.location.href='" + gstLink + "'", true);

    }
    void LoadNhanVien()
    {
        drnhanvien.Items.Clear();
        drnhanvien.DataSource = ex.GetData("sp_ListNhanVien_ThanhToanTienMat");
        drnhanvien.DataBind();
        drnhanvien.Items.Insert(0, new ListItem("--Chọn nhân viên--", ""));
        drnhanvien.SelectedIndex = 0;
    }

    void LoadDoiTuongChi()
    {
        dldoituong.Items.Clear();
        dldoituong.DataSource = ex.GetData("sp_XDANoiDungTTTienMat_DoituongChi");
        dldoituong.DataBind();
        dldoituong.Items.Insert(0, new ListItem("--Chọn đối tượng chi--", ""));
        dldoituong.SelectedIndex = 0;
    }

    void LoadNhanVien_TimKiem()
    {
        dropNhanVien.Items.Clear();
        dropNhanVien.DataSource = ex.GetData("sp_ListNhanVien_ThanhToanTienMat_TimKiem");
        dropNhanVien.DataBind();
        dropNhanVien.Items.Insert(0, new ListItem("--Chọn nhân viên--", ""));
        dropNhanVien.SelectedIndex = 0;
    }
    protected void drnhanvien_SelectedIndexChanged(object sender, EventArgs e)
    {
        p = new SqlParameter[1];
        p[0] = new SqlParameter("@IDs", ex.GetGuid(drnhanvien.SelectedValue));
        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
        DataTable dt = ex.GetData("sp_ListBoPhan_ChucVu", p);
        if (dt.Rows.Count > 0)
        {
            try
            {
                txtbophan.Text = dt.Rows[0]["tenbophan"].ToString();
                txtchucvu.Text = dt.Rows[0]["tenchucvu"].ToString();
            }
            catch (Exception ax)
            {
                gstGetMess("Có lỗi trong quá trình xem, xem trong file log", "");
                ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tThiết lập phiếu chi tiền mặt\t" + ax.Message);
            }
        }
        ListPhieuChiTheoNhanVien();
        ListPhieuChiTon();
    }

    public void ListPhieuChiTon()
    {
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
        p[0].SqlDbType = SqlDbType.SmallDateTime;
        p[1] = new SqlParameter("@NhanVien", ex.GetGuid(drnhanvien.SelectedValue));
        p[1].SqlDbType = SqlDbType.UniqueIdentifier;
        drphieuchiton.Items.Clear();
        drphieuchiton.DataSource = ex.GetData("sp_TLThanhToanTienMat_ListPhieuChi", p);
        drphieuchiton.DataBind();
        drphieuchiton.Items.Insert(0, new ListItem("--Chọn phiếu chi tồn--", ""));
        drphieuchiton.SelectedIndex = 0;
    }

    public void ListPhieuChiTheoNhanVien()
    {
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
        p[0].SqlDbType = SqlDbType.SmallDateTime;
        p[1] = new SqlParameter("@NhanVien", ex.GetGuid(drnhanvien.SelectedValue));
        p[1].SqlDbType = SqlDbType.UniqueIdentifier;
        dt = ex.GetData("sp_TLThanhToanTienMat_ListPhieuChi", p);
        if (dt.Rows.Count > 0)
        {
            drphieuchi.DataSource = dt;
            drphieuchi.DataTextField = "sopc";
            drphieuchi.DataValueField = "idlenhchi";
            drphieuchi.DataBind();
        }
        else
        {
            drphieuchi.DataSource = null;
            drphieuchi.DataTextField = null;
            drphieuchi.DataValueField = null;
            drphieuchi.DataBind();
        }
    }

    public void ListPhieuChiTon_TinhChoChiHa()
    {
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
        p[0].SqlDbType = SqlDbType.SmallDateTime;
        p[1] = new SqlParameter("@NhanVien", ex.GetGuid(drnhanvien.SelectedValue));
        p[1].SqlDbType = SqlDbType.UniqueIdentifier;
        drphieuchiton.Items.Clear();
        drphieuchiton.DataSource = ex.GetData("sp_TLThanhToanTienMat_ListPhieuChi_TinhChoChiHa", p);
        drphieuchiton.DataBind();
        drphieuchiton.Items.Insert(0, new ListItem("--Chọn phiếu chi tồn--", ""));
        drphieuchiton.SelectedIndex = 0;
    }

    public void ListPhieuChiTheoNhanVien_TinhChoChiHa()
    {
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
        p[0].SqlDbType = SqlDbType.SmallDateTime;
        p[1] = new SqlParameter("@NhanVien", ex.GetGuid(drnhanvien.SelectedValue));
        p[1].SqlDbType = SqlDbType.UniqueIdentifier;
        dt = ex.GetData("sp_TLThanhToanTienMat_ListPhieuChi_TinhChoChiHa", p);
        if (dt.Rows.Count > 0)
        {
            drphieuchi.DataSource = dt;
            drphieuchi.DataTextField = "sopc";
            drphieuchi.DataValueField = "idlenhchi";
            drphieuchi.DataBind();
        }
        else
        {
            drphieuchi.DataSource = null;
            drphieuchi.DataTextField = null;
            drphieuchi.DataValueField = null;
            drphieuchi.DataBind();
        }
    }

    protected void drphieuchi_SelectedIndexChanged(object sender, EventArgs e)
    {

        tongtien = 0;
        foreach (ListItem item in (sender as ListControl).Items)
        {

            if (item.Selected)
            {
                Guid id = new Guid(item.Value);
                p = new SqlParameter[1];
                p[0] = new SqlParameter("@IDLenhChi", id);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;

                dt = ex.GetData("sp_TLThanhToanTienMat_TienPhieuChi", p);
                if (dt.Rows.Count > 0)
                {
                    t = double.Parse(dt.Rows[0]["sotien"].ToString());
                }
                else t = 0;
            }
            tongtien = tongtien + t;
            t = 0;
        }

        txtsotien.Text = tongtien.ToString();
        if (tongtienh.Text == "")
            txtsotienton.Text = (double.Parse(txtsotien.Text) - 0).ToString();
        else
            txtsotienton.Text = (double.Parse(txtsotien.Text) - double.Parse(tongtienh.Text)).ToString();
        LoadTienTon(sender, e);
    }
    string ConvertDecimal(double s)
    {
        string q = "";
        if (s.ToString().Contains("."))
        {
            double x = double.Parse(s.ToString().Substring(0, s.ToString().IndexOf('.')));
            q = (x != 0 && x > 999) ? string.Format("{0:0,0}", x) : x.ToString();
            q += s.ToString().Substring(s.ToString().IndexOf('.'));
        }
        else
            q = (s != 0 && s > 999) ? string.Format("{0:0,0}", s) : s.ToString();
        return q;
    }
    private void AddNewRow()
    {
        int rowIndex = 0;

        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    TextBox txtnoidung = (TextBox)GV1.Rows[rowIndex].Cells[1].FindControl("txtnoidung");
                    DropDownList dlhangmuc = (DropDownList)GV1.Rows[rowIndex].Cells[2].FindControl("dlhangmuc");
                    DropDownList dldoituong = (DropDownList)GV1.Rows[rowIndex].Cells[3].FindControl("dldoituong");
                    DropDownList dlnhacungcap = (DropDownList)GV1.Rows[rowIndex].Cells[4].FindControl("drnhacungcap");
                    DropDownList dlnhomvatlieu = (DropDownList)GV1.Rows[rowIndex].Cells[5].FindControl("drnhomvatlieu");
                    DropDownList dlloaivatlieu = (DropDownList)GV1.Rows[rowIndex].Cells[6].FindControl("drloaivatlieu");
                    DropDownList dldonvi = (DropDownList)GV1.Rows[rowIndex].Cells[7].FindControl("dldonvi");
                    TextBox txtsoluong = (TextBox)GV1.Rows[rowIndex].Cells[8].FindControl("txtsoluong");
                    TextBox txtdongia = (TextBox)GV1.Rows[rowIndex].Cells[9].FindControl("txtdongia");
                    TextBox txtthanhtien = (TextBox)GV1.Rows[rowIndex].Cells[11].FindControl("txtthanhtien");
                    TextBox txtmathietbi = (TextBox)GV1.Rows[rowIndex].Cells[12].FindControl("txtmathietbi");
                    CheckBox ckVAT = (CheckBox)GV1.Rows[rowIndex].Cells[13].FindControl("ckVAT");
                    TextBox txtthanhtienhang = (TextBox)GV1.Rows[rowIndex].Cells[10].FindControl("txtthanhtienhang");
                    CheckBox cknhapkho = (CheckBox)GV1.Rows[rowIndex].Cells[14].FindControl("cknhapkho");
                    CheckBox cktinhchochiha = (CheckBox)GV1.Rows[rowIndex].Cells[15].FindControl("cktinhchochiha");
                    CheckBox cktaisancodinh = (CheckBox)GV1.Rows[rowIndex].Cells[15].FindControl("cktaisancodinh");
                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = i + 1;
                    dtCurrentTable.Rows[i - 1]["Noidung"] = txtnoidung.Text.Trim();
                    dtCurrentTable.Rows[i - 1]["DoiTuong"] = dldoituong.SelectedValue;
                    dtCurrentTable.Rows[i - 1]["DonVi"] = dldonvi.SelectedValue;
                    dtCurrentTable.Rows[i - 1]["HangMuc"] = dlhangmuc.SelectedValue;
                    dtCurrentTable.Rows[i - 1]["SoLuong"] = txtsoluong.Text.Trim();
                    dtCurrentTable.Rows[i - 1]["DonGia"] = txtdongia.Text.Trim();
                    dtCurrentTable.Rows[i - 1]["ThanhTien"] = txtthanhtien.Text.Trim();
                    dtCurrentTable.Rows[i - 1]["CoVAT"] = ckVAT.Checked.ToString();
                    dtCurrentTable.Rows[i - 1]["ThanhTienHang"] = txtthanhtienhang.Text.Trim();
                    dtCurrentTable.Rows[i - 1]["NhapKho"] = cknhapkho.Checked.ToString();
                    dtCurrentTable.Rows[i - 1]["NhaCungCap"] = dlnhacungcap.SelectedValue;
                    dtCurrentTable.Rows[i - 1]["NhomVatLieu"] = dlnhomvatlieu.SelectedValue;
                    dtCurrentTable.Rows[i - 1]["LoaiVatLieu"] = dlloaivatlieu.SelectedValue;
                    dtCurrentTable.Rows[i - 1]["MaThietBi"] = txtmathietbi.Text.Trim();
                    dtCurrentTable.Rows[i - 1]["TinhChoChiHa"] = cktinhchochiha.Checked.ToString();
                    dtCurrentTable.Rows[i - 1]["TaiSanCoDinh"] = cktaisancodinh.Checked.ToString();
                    rowIndex++;
                }
                dtCurrentTable.Rows.Add(drCurrentRow);
                ViewState["CurrentTable"] = dtCurrentTable;

                GV1.DataSource = dtCurrentTable;
                GV1.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }
        SetPreviousData();

    }
    private void SetPreviousData()
    {
        int rowIndex = 0;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    
                    TextBox txtnoidung = (TextBox)GV1.Rows[rowIndex].Cells[1].FindControl("txtnoidung");
                    DropDownList dlhangmuc = (DropDownList)GV1.Rows[rowIndex].Cells[2].FindControl("dlhangmuc");
                    DropDownList dldoituong = (DropDownList)GV1.Rows[rowIndex].Cells[3].FindControl("dldoituong");
                    DropDownList dlnhacungcap = (DropDownList)GV1.Rows[rowIndex].Cells[4].FindControl("drnhacungcap");
                    DropDownList dlnhomvatlieu = (DropDownList)GV1.Rows[rowIndex].Cells[5].FindControl("drnhomvatlieu");
                    DropDownList dlloaivatlieu = (DropDownList)GV1.Rows[rowIndex].Cells[6].FindControl("drloaivatlieu");
                    DropDownList dldonvi = (DropDownList)GV1.Rows[rowIndex].Cells[7].FindControl("dldonvi");
                    TextBox txtsoluong = (TextBox)GV1.Rows[rowIndex].Cells[8].FindControl("txtsoluong");
                    TextBox txtdongia = (TextBox)GV1.Rows[rowIndex].Cells[9].FindControl("txtdongia");
                    TextBox txtthanhtienhang = (TextBox)GV1.Rows[rowIndex].Cells[10].FindControl("txtthanhtienhang");
                    TextBox txtthanhtien = (TextBox)GV1.Rows[rowIndex].Cells[11].FindControl("txtthanhtien");
                    TextBox txtmathietbi = (TextBox)GV1.Rows[rowIndex].Cells[12].FindControl("txtmathietbi");
                    CheckBox ckVAT = (CheckBox)GV1.Rows[rowIndex].Cells[13].FindControl("ckVAT");

                    CheckBox cknhapkho = (CheckBox)GV1.Rows[rowIndex].Cells[14].FindControl("cknhapkho");
                    CheckBox cktinhchochiha = (CheckBox)GV1.Rows[rowIndex].Cells[15].FindControl("cktinhchochiha");
                    CheckBox cktaisancodinh = (CheckBox)GV1.Rows[rowIndex].Cells[16].FindControl("cktaisancodinh");
                    GV1.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
                    txtnoidung.Focus();
                    txtnoidung.Text = dt.Rows[i]["Noidung"].ToString();
                    dlhangmuc.SelectedValue = dt.Rows[i]["HangMuc"].ToString();
                    dldoituong.SelectedValue = dt.Rows[i]["DoiTuong"].ToString();
                    dlnhacungcap.SelectedValue = dt.Rows[i]["NhaCungCap"].ToString();
                    dlnhomvatlieu.SelectedValue = dt.Rows[i]["NhomVatLieu"].ToString();
                    dlloaivatlieu.SelectedValue = dt.Rows[i]["LoaiVatLieu"].ToString();
                    dldonvi.SelectedValue = dt.Rows[i]["DonVi"].ToString();
                    txtsoluong.Text = dt.Rows[i]["SoLuong"].ToString();
                    txtdongia.Text = dt.Rows[i]["DonGia"].ToString();
                    txtmathietbi.Text = dt.Rows[i]["MaThietBi"].ToString();
                    txtthanhtien.Text = dt.Rows[i]["ThanhTien"].ToString();
                    txtthanhtienhang.Text = dt.Rows[i]["ThanhTienHang"].ToString();
                    string vat = dt.Rows[i]["CoVAT"].ToString();
                    string tinhchochiha = dt.Rows[i]["TinhChoChiHa"].ToString();
                    string nhapkho = dt.Rows[i]["NhapKho"].ToString();
                    string taisancodinh = dt.Rows[i]["TaiSanCoDinh"].ToString();
                    ckVAT.Checked = vat.Equals("") ? false : bool.Parse(dt.Rows[i]["CoVAT"].ToString());
                    cktinhchochiha.Checked = tinhchochiha.Equals("") ? false : bool.Parse(dt.Rows[i]["TinhChoChiHa"].ToString());
                    cknhapkho.Checked = nhapkho.Equals("") ? false : bool.Parse(dt.Rows[i]["NhapKho"].ToString());
                    cktaisancodinh.Checked = taisancodinh.Equals("") ? false : bool.Parse(dt.Rows[i]["TaiSanCoDinh"].ToString());
                    rowIndex++;
                }
            }
        }
    }
    private void FirstGridViewRow()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        dt.Columns.Add(new DataColumn("Noidung", typeof(string)));
        dt.Columns.Add(new DataColumn("HangMuc", typeof(string)));
        dt.Columns.Add(new DataColumn("NhaCungCap", typeof(string)));
        dt.Columns.Add(new DataColumn("DoiTuong", typeof(string)));
        dt.Columns.Add(new DataColumn("DonVi", typeof(string)));
        dt.Columns.Add(new DataColumn("SoLuong", typeof(string)));
        dt.Columns.Add(new DataColumn("DonGia", typeof(string)));
        dt.Columns.Add(new DataColumn("ThanhTien", typeof(string)));
        dt.Columns.Add(new DataColumn("CoVAT", typeof(bool)));
        dt.Columns.Add(new DataColumn("ThanhTienHang", typeof(string)));
        dt.Columns.Add(new DataColumn("NhapKho", typeof(bool)));
        dt.Columns.Add(new DataColumn("NhomVatLieu", typeof(string)));
        dt.Columns.Add(new DataColumn("MaThietBi", typeof(string)));
        dt.Columns.Add(new DataColumn("LoaiVatLieu", typeof(string)));
        dt.Columns.Add(new DataColumn("TinhChoChiHa", typeof(bool)));
        dt.Columns.Add(new DataColumn("TaiSanCoDinh", typeof(bool)));
        dr = dt.NewRow();
        dr["RowNumber"] = 1;
        dr["Noidung"] = string.Empty;
        dr["NhaCungCap"] = string.Empty;
        dr["LoaiVatLieu"] = string.Empty;
        dr["NhomVatLieu"] = string.Empty;
        dr["MaThietBi"] = string.Empty;
        dr["HangMuc"] = string.Empty;
        dr["DoiTuong"] = string.Empty;
        dr["DonVi"] = string.Empty;
        dr["SoLuong"] = string.Empty;
        dr["DonGia"] = string.Empty;
        dr["ThanhTien"] = string.Empty;
        dr["TinhChoChiHa"] = false;
        dr["CoVAT"] = false;
        dr["ThanhTienHang"] = string.Empty;
        dr["NhapKho"] = false;
        dr["TaiSanCoDinh"] = false;
        dt.Rows.Add(dr);

        ViewState["CurrentTable"] = dt;


        GV1.DataSource = dt;
        GV1.DataBind();
    }
    protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        SetRowData();
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            int rowIndex = Convert.ToInt32(e.RowIndex);
            if (dt.Rows.Count > 1)
            {
                dt.Rows.Remove(dt.Rows[rowIndex]);
                drCurrentRow = dt.NewRow();
                ViewState["CurrentTable"] = dt;
                GV1.DataSource = dt;
                GV1.DataBind();

                for (int i = 0; i < GV1.Rows.Count - 1; i++)
                {
                    GV1.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
                }
                SetPreviousData();
            }
        }
    }
    private void SetRowData()
    {
        //int rowIndex = 0;

        //if (ViewState["CurrentTable"] != null)
        //{
        //    DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
        //    DataRow drCurrentRow = null;
        //    if (dtCurrentTable.Rows.Count > 0)
        //    {
        //        for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
        //        {
        //            TextBox txtnoidung = (TextBox)GV1.Rows[rowIndex].Cells[1].FindControl("txtnoidung");

        //            drCurrentRow = dtCurrentTable.NewRow();
        //            drCurrentRow["RowNumber"] = i + 1;
        //            dtCurrentTable.Rows[i - 1]["Noidung"] = txtnoidung.Text.Trim();

        //            rowIndex++;
        //        }

        //        ViewState["CurrentTable"] = dtCurrentTable;
        //    }
        //}
        //else
        //{
        //    Response.Write("ViewState is null");
        //}
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        AddNewRow();
    }
    public void LoadTienTon(object sender, EventArgs e)
    {
        if (double.Parse(txtsotienton.Text.Trim()) == 0)
        {
            drphieuchiton.Visible = false;
        }
        else drphieuchiton.Visible = true;
    }
    protected void txtngaythang_TextChanged(object sender, EventArgs e)
    {
        ListPhieuChiTheoNhanVien();
    }
    protected void btnSave_Click1(object sender, EventArgs e)
    {
        if (f_Them.Equals(1))
        {
            try
            {
                for (int j = 0; j < GV1.Rows.Count;j++)
                {
                    DropDownList dldoituong1 = (DropDownList)GV1.Rows[j].Cells[3].FindControl("dldoituong");
                    if (dldoituong1.SelectedIndex == 0)
                        gstGetMess("Chon đối tượng chị", "");
                    else
                    {

                        if (double.Parse(txtsotienton.Text.Trim()) != 0 && drphieuchiton.SelectedIndex == 0)
                            gstGetMess("Tồn tại số tiền tồn.Chọn phiếu chi tồn", "");
                        else
                            if (double.Parse(txtsotienton.Text.Trim()) == 0 && drphieuchiton.SelectedIndex > 0)
                                gstGetMess("Không chọn phiếu tồn", "");
                            else
                            {
                                p = new SqlParameter[1];
                                p[0] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                                p[0].SqlDbType = SqlDbType.SmallDateTime;
                                dt = ex.GetData("sp_xdagetsophieuThanhToan_TienMat", p);
                                int sopt = Int16.Parse(dt.Rows[0]["sopc"].ToString());
                                if (sopt == 0)
                                    sopt = 1;
                                else sopt = sopt + 1;
                                Guid idthanhtoan = Guid.NewGuid();
                                p = new SqlParameter[7];
                                p[0] = new SqlParameter("@IDs", idthanhtoan);
                                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                                p[1] = new SqlParameter("@NhanVien", ex.GetGuid(drnhanvien.SelectedValue));
                                p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                                p[2] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                                p[2].SqlDbType = SqlDbType.SmallDateTime;
                                p[3] = new SqlParameter("@IDPhieuChi", DBNull.Value);
                                p[3].SqlDbType = SqlDbType.UniqueIdentifier;
                                p[4] = new SqlParameter("@TonTheoPhieuChi", double.Parse(txtsotien.Text.Trim().Equals("") ? "0" : txtsotien.Text.Trim()));
                                p[4].SqlDbType = SqlDbType.Float;
                                p[5] = new SqlParameter("@Del", 1);
                                p[5].SqlDbType = SqlDbType.Int;
                                p[6] = new SqlParameter("@SoPhieuTT", sopt);
                                p[6].SqlDbType = SqlDbType.Int;
                                ex.ExecuteNonQuerry("sp_xdathanhtoantienmat_Add", p);

                                for (int i = 0; i < GV1.Rows.Count; i++)
                                {

                                    TextBox txtnoidung = (TextBox)GV1.Rows[i].Cells[1].FindControl("txtnoidung");
                                    DropDownList dlhangmuc = (DropDownList)GV1.Rows[i].Cells[2].FindControl("dlhangmuc");
                                    DropDownList dldoituong = (DropDownList)GV1.Rows[i].Cells[3].FindControl("dldoituong");
                                    DropDownList dlnhacungcap = (DropDownList)GV1.Rows[i].Cells[4].FindControl("drnhacungcap");
                                    DropDownList dlnhomvatlieu = (DropDownList)GV1.Rows[i].Cells[5].FindControl("drnhomvatlieu");
                                    DropDownList dlloaivatlieu = (DropDownList)GV1.Rows[i].Cells[6].FindControl("drloaivatlieu");
                                    DropDownList dldonvi = (DropDownList)GV1.Rows[i].Cells[7].FindControl("dldonvi");
                                    TextBox txtsoluong = (TextBox)GV1.Rows[i].Cells[8].FindControl("txtsoluong");
                                    TextBox txtdongia = (TextBox)GV1.Rows[i].Cells[9].FindControl("txtdongia");
                                    TextBox txtthanhtienhang = (TextBox)GV1.Rows[i].Cells[10].FindControl("txtthanhtienhang");
                                    TextBox txtthanhtien = (TextBox)GV1.Rows[i].Cells[11].FindControl("txtthanhtien");
                                    TextBox txtmathietbi = (TextBox)GV1.Rows[i].Cells[12].FindControl("txtmathietbi");
                                    CheckBox ckVAT = (CheckBox)GV1.Rows[i].Cells[13].FindControl("ckVAT");

                                    CheckBox cknhapkho = (CheckBox)GV1.Rows[i].Cells[14].FindControl("cknhapkho");
                                    CheckBox cktinhchochiha = (CheckBox)GV1.Rows[i].Cells[15].FindControl("cktinhchochiha");
                                    CheckBox cktaisancodinh = (CheckBox)GV1.Rows[i].Cells[15].FindControl("cktaisancodinh");
                                    p = new SqlParameter[20];
                                    p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                                    p[1] = new SqlParameter("@NoiDung", txtnoidung.Text.Trim());
                                    p[1].SqlDbType = SqlDbType.NVarChar;
                                    p[2] = new SqlParameter("@DoiTuong", ex.GetGuid(dldoituong.SelectedValue));
                                    p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                                    p[3] = new SqlParameter("@DonVi", ex.GetGuid(dldonvi.SelectedValue));
                                    p[3].SqlDbType = SqlDbType.UniqueIdentifier;
                                    p[4] = new SqlParameter("@HangMuc", ex.GetGuid(dlhangmuc.SelectedValue));
                                    p[4].SqlDbType = SqlDbType.UniqueIdentifier;
                                    p[5] = new SqlParameter("@SoLuong", double.Parse(txtsoluong.Text.Trim().Equals("") ? "0" : txtsoluong.Text.Trim()));
                                    p[5].SqlDbType = SqlDbType.Float;
                                    p[6] = new SqlParameter("@DonGia", double.Parse(txtdongia.Text.Trim().Equals("") ? "0" : txtdongia.Text.Trim()));
                                    p[6].SqlDbType = SqlDbType.Float;
                                    p[7] = new SqlParameter("@TienThanhToan", double.Parse(txtthanhtien.Text.Trim().Equals("") ? "0" : txtthanhtien.Text.Trim()));
                                    p[7].SqlDbType = SqlDbType.Float;
                                    p[8] = new SqlParameter("@CoVAT", ckVAT.Checked ? 1 : 2);
                                    p[8].SqlDbType = SqlDbType.Int;
                                    p[9] = new SqlParameter("@CoHoaDon", DBNull.Value);
                                    p[9].SqlDbType = SqlDbType.Int;
                                    p[10] = new SqlParameter("@IDThanhToan", idthanhtoan);
                                    p[10].SqlDbType = SqlDbType.UniqueIdentifier;
                                    p[11] = new SqlParameter("@Del", 1);
                                    p[11].SqlDbType = SqlDbType.Int;
                                    p[12] = new SqlParameter("@NhaCungCap", ex.GetGuid(dlnhacungcap.SelectedValue));
                                    p[12].SqlDbType = SqlDbType.UniqueIdentifier;
                                    p[13] = new SqlParameter("@NhomVatLieu", ex.GetGuid(dlnhomvatlieu.SelectedValue));
                                    p[13].SqlDbType = SqlDbType.UniqueIdentifier;
                                    p[14] = new SqlParameter("@LoaiVatLieu", ex.GetGuid(dlloaivatlieu.SelectedValue));
                                    p[14].SqlDbType = SqlDbType.UniqueIdentifier;
                                    p[15] = new SqlParameter("@MaThietBi", txtmathietbi.Text.Trim());
                                    p[15].SqlDbType = SqlDbType.NVarChar;
                                    p[16] = new SqlParameter("@TinhChoChiHa", cktinhchochiha.Checked ? 1 : 2);
                                    p[16].SqlDbType = SqlDbType.Int;
                                    p[17] = new SqlParameter("@TaiSanCoDinh", cktaisancodinh.Checked ? 1 : 2);
                                    p[17].SqlDbType = SqlDbType.Int;
                                    p[18] = new SqlParameter("@NhapKho", cknhapkho.Checked ? 1 : 2);
                                    p[18].SqlDbType = SqlDbType.Int;
                                    p[19] = new SqlParameter("@ThanhTienHang", double.Parse(txtthanhtienhang.Text.Trim().Equals("") ? "0" : txtthanhtienhang.Text.Trim()));
                                    p[19].SqlDbType = SqlDbType.Float;
                                    ex.ExecuteNonQuerry("sp_XDANoiDungTTTienMat_Add", p);
                                }

                                if (txtsotienton.Text.Equals("0") && drphieuchiton.SelectedIndex == 0)
                                {
                                    foreach (System.Web.UI.WebControls.ListItem item in drphieuchi.Items)
                                    {
                                        if (item.Selected)
                                        {
                                            Guid id = new Guid(item.Value);
                                            p = new SqlParameter[1];
                                            p[0] = new SqlParameter("@idlenhchi", id);
                                            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                                            dt = ex.GetData("sp_TLThanhToanTienMat_TienPhieuChi", p);
                                            if (dt.Rows.Count > 0)
                                            {
                                                double sotien = double.Parse(dt.Rows[0]["sotien"].ToString());
                                                p = new SqlParameter[5];
                                                p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                                                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                                                p[1] = new SqlParameter("@IDThanhToan", idthanhtoan);
                                                p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                                                p[2] = new SqlParameter("@IDPhieuChi", id);
                                                p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                                                p[3] = new SqlParameter("@SoTienThanhToan", sotien);
                                                p[3].SqlDbType = SqlDbType.Float;
                                                p[4] = new SqlParameter("@Del", 1);
                                                p[4].SqlDbType = SqlDbType.Int;
                                                ex.ExecuteNonQuerry("sp_XDAChiTietThanhToan_Add", p);
                                            }
                                        }

                                    }
                                }
                                else if (double.Parse(txtsotienton.Text) != 0)
                                {
                                    foreach (System.Web.UI.WebControls.ListItem item in drphieuchi.Items)
                                    {
                                        if (item.Selected)
                                        {
                                            if (!item.Value.Equals(drphieuchiton.SelectedValue))
                                            {
                                                Guid id = new Guid(item.Value);
                                                p = new SqlParameter[1];
                                                p[0] = new SqlParameter("@idlenhchi", id);
                                                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                                                dt = ex.GetData("sp_TLThanhToanTienMat_TienPhieuChi", p);
                                                if (dt.Rows.Count > 0)
                                                {
                                                    double sotien = double.Parse(dt.Rows[0]["sotien"].ToString());
                                                    p = new SqlParameter[5];
                                                    p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                                                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                                                    p[1] = new SqlParameter("@IDThanhToan", idthanhtoan);
                                                    p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                                                    p[2] = new SqlParameter("@IDPhieuChi", id);
                                                    p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                                                    p[3] = new SqlParameter("@SoTienThanhToan", sotien);
                                                    p[3].SqlDbType = SqlDbType.Float;
                                                    p[4] = new SqlParameter("@Del", 1);
                                                    p[4].SqlDbType = SqlDbType.Int;
                                                    ex.ExecuteNonQuerry("sp_XDAChiTietThanhToan_Add", p);
                                                }
                                            }
                                            else
                                            {
                                                Guid id = new Guid(item.Value);
                                                p = new SqlParameter[1];
                                                p[0] = new SqlParameter("@idlenhchi", id);
                                                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                                                dt = ex.GetData("sp_TLThanhToanTienMat_TienPhieuChi", p);
                                                if (dt.Rows.Count > 0)
                                                {
                                                    double sotien = double.Parse(dt.Rows[0]["sotien"].ToString());
                                                    double tienton = double.Parse(txtsotienton.Text.Trim());
                                                    p = new SqlParameter[5];
                                                    p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                                                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                                                    p[1] = new SqlParameter("@IDThanhToan", idthanhtoan);
                                                    p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                                                    p[2] = new SqlParameter("@IDPhieuChi", id);
                                                    p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                                                    p[3] = new SqlParameter("@SoTienThanhToan", sotien - double.Parse(txtsotienton.Text.Trim()));
                                                    p[3].SqlDbType = SqlDbType.Float;
                                                    p[4] = new SqlParameter("@Del", 1);
                                                    p[4].SqlDbType = SqlDbType.Int;
                                                    ex.ExecuteNonQuerry("sp_XDAChiTietThanhToan_Add", p);
                                                }
                                            }
                                        }

                                    }

                                }

                                if (ckhoantheophieuchi.Checked == true)
                                {
                                    tienton = double.Parse(txtsotienton.Text.Trim());
                                    if (tienton < 0)
                                    {
                                        Guid tlchi = Guid.NewGuid();
                                        p = new SqlParameter[12];
                                        p[0] = new SqlParameter("@IDs", tlchi);
                                        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[1] = new SqlParameter("@NhanVien", ex.GetGuid(drnhanvien.SelectedValue));
                                        p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[2] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                                        p[2].SqlDbType = SqlDbType.SmallDateTime;
                                        p[3] = new SqlParameter("@SoTien", double.Parse(txtsotienhoanlai.Text.Trim().Equals("") ? "0" : txtsotienhoanlai.Text.Trim()));
                                        p[3].SqlDbType = SqlDbType.Float;
                                        p[4] = new SqlParameter("@NoiDung", "Thanh toán bù cho phiếu chi:" + drphieuchiton.SelectedItem.Text);
                                        p[4].SqlDbType = SqlDbType.NVarChar;

                                        p[5] = new SqlParameter("@NgayTao", DateTime.Now);
                                        p[5].SqlDbType = SqlDbType.SmallDateTime;
                                        p[6] = new SqlParameter("@NguoiTao", Session["user"].ToString());
                                        p[6].SqlDbType = SqlDbType.NVarChar;
                                        p[7] = new SqlParameter("@Del", 1);
                                        p[7].SqlDbType = SqlDbType.Int;
                                        p[8] = new SqlParameter("@IDThanhToan", idthanhtoan);
                                        p[8].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[9] = new SqlParameter("@UngLuong", DBNull.Value);
                                        p[9].SqlDbType = SqlDbType.Int;
                                        p[10] = new SqlParameter("@TinhChoChiHa", DBNull.Value);
                                        p[10].SqlDbType = SqlDbType.Int;
                                        p[11] = new SqlParameter("@NopNganHang", DBNull.Value);
                                        p[11].SqlDbType = SqlDbType.Int;
                                        ex.ExecuteNonQuerry("sp_xdaThietLapPhieuChiTienMat_Add", p);


                                        p = new SqlParameter[1];
                                        p[0] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                                        p[0].SqlDbType = SqlDbType.SmallDateTime;
                                        dt = ex.GetData("sp_xdagetsophieuchiconnotravat_TienMat", p);
                                        int sopc = Int16.Parse(dt.Rows[0]["sopc"].ToString());
                                        if (sopc == 0)
                                            pc = 1;
                                        else pc = sopc + 1;
                                        p = new SqlParameter[11];
                                        p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                                        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[1] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                                        p[1].SqlDbType = SqlDbType.SmallDateTime;
                                        p[2] = new SqlParameter("@NoiDung", "Thanh toán bù cho phiếu chi:" + drphieuchiton.SelectedItem.Text);
                                        p[2].SqlDbType = SqlDbType.NVarChar;
                                        p[3] = new SqlParameter("@DonVi", DBNull.Value);
                                        p[3].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[4] = new SqlParameter("@SoPC", pc);
                                        p[4].SqlDbType = SqlDbType.Int;
                                        p[5] = new SqlParameter("@TaiKhoan", DBNull.Value);
                                        p[5].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[6] = new SqlParameter("@Del", 1);
                                        p[6].SqlDbType = SqlDbType.Int;
                                        p[7] = new SqlParameter("@LoaiCongNo", 2);
                                        p[7].SqlDbType = SqlDbType.Int;
                                        p[8] = new SqlParameter("@SoTien", double.Parse(txtsotienhoanlai.Text.Trim().Equals("") ? "0" : txtsotienhoanlai.Text.Trim()));
                                        p[8].SqlDbType = SqlDbType.Float;
                                        p[9] = new SqlParameter("@IDLenhChi", tlchi);
                                        p[9].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[10] = new SqlParameter("@NhanVien", ex.GetGuid(drnhanvien.SelectedValue));
                                        p[10].SqlDbType = SqlDbType.UniqueIdentifier;
                                        ex.ExecuteNonQuerry("sp_xdaphieuchicongno_Add", p);

                                        p = new SqlParameter[6];
                                        p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                                        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[1] = new SqlParameter("@IDThanhToan", idthanhtoan);
                                        p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[2] = new SqlParameter("@IDPhieuChi", ex.GetGuid(drphieuchiton.SelectedValue));
                                        p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[3] = new SqlParameter("@SoTien", double.Parse(txtsotienhoanlai.Text.Trim().Equals("") ? "0" : txtsotienhoanlai.Text.Trim()));
                                        p[3].SqlDbType = SqlDbType.Float;
                                        p[4] = new SqlParameter("@LoaiTon", 1);
                                        p[4].SqlDbType = SqlDbType.Int;
                                        p[5] = new SqlParameter("@Del", 1);
                                        p[5].SqlDbType = SqlDbType.Int;
                                        ex.ExecuteNonQuerry("sp_xdaphieuchiton_Add", p);
                                        p = new SqlParameter[4];
                                        p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                                        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[1] = new SqlParameter("@NoiDung", "Thanh toán bù cho phiếu chi:" + drphieuchiton.SelectedItem.Text);
                                        p[1].SqlDbType = SqlDbType.NVarChar;
                                        p[2] = new SqlParameter("@IDPhieuChi", tlchi);
                                        p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[3] = new SqlParameter("@Del", 1);
                                        p[3].SqlDbType = SqlDbType.Int;
                                        ex.ExecuteNonQuerry("sp_xdanoidungpc_Add", p);

                                    }
                                    else
                                    {
                                        Guid tlchi = Guid.NewGuid();
                                        p = new SqlParameter[9];
                                        p[0] = new SqlParameter("@IDs", tlchi);
                                        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[1] = new SqlParameter("@NhanVien", ex.GetGuid(drnhanvien.SelectedValue));
                                        p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[2] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                                        p[2].SqlDbType = SqlDbType.SmallDateTime;
                                        p[3] = new SqlParameter("@SoTien", double.Parse(txtsotienhoanlai.Text.Trim().Equals("") ? "0" : txtsotienhoanlai.Text.Trim()));
                                        p[3].SqlDbType = SqlDbType.Float;
                                        p[4] = new SqlParameter("@NoiDung", "Thanh toán bù cho phiếu chi:" + drphieuchiton.SelectedItem.Text);
                                        p[4].SqlDbType = SqlDbType.NVarChar;

                                        p[5] = new SqlParameter("@NgayTao", DateTime.Now);
                                        p[5].SqlDbType = SqlDbType.SmallDateTime;
                                        p[6] = new SqlParameter("@NguoiTao", Session["user"].ToString());
                                        p[6].SqlDbType = SqlDbType.NVarChar;
                                        p[7] = new SqlParameter("@Del", 1);
                                        p[7].SqlDbType = SqlDbType.Int;
                                        p[8] = new SqlParameter("@IDThanhToan", idthanhtoan);
                                        p[8].SqlDbType = SqlDbType.UniqueIdentifier;
                                        ex.ExecuteNonQuerry("sp_XDAThietLapPhieuThuTienMat_Add", p);


                                        p = new SqlParameter[1];
                                        p[0] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                                        p[0].SqlDbType = SqlDbType.SmallDateTime;
                                        dt = ex.GetData("sp_xdagetsophieuthuconnotravat_TienMat", p);
                                        int sopc = Int16.Parse(dt.Rows[0]["sopc"].ToString());
                                        if (sopc == 0)
                                            pc = 1;
                                        else pc = sopc + 1;
                                        p = new SqlParameter[11];
                                        p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                                        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[1] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                                        p[1].SqlDbType = SqlDbType.SmallDateTime;
                                        p[2] = new SqlParameter("@NoiDung", "Thanh toán bù cho phiếu chi:" + drphieuchi.SelectedItem.Text);
                                        p[2].SqlDbType = SqlDbType.NVarChar;
                                        p[3] = new SqlParameter("@DonVi", DBNull.Value);
                                        p[3].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[4] = new SqlParameter("@SoPC", pc);
                                        p[4].SqlDbType = SqlDbType.Int;
                                        p[5] = new SqlParameter("@TaiKhoan", DBNull.Value);
                                        p[5].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[6] = new SqlParameter("@Del", 1);
                                        p[6].SqlDbType = SqlDbType.Int;
                                        p[7] = new SqlParameter("@LoaiCongNo", 2);
                                        p[7].SqlDbType = SqlDbType.Int;
                                        p[8] = new SqlParameter("@SoTien", double.Parse(txtsotienhoanlai.Text.Trim().Equals("") ? "0" : txtsotienhoanlai.Text.Trim()));
                                        p[8].SqlDbType = SqlDbType.Float;
                                        p[9] = new SqlParameter("@IDLenhChi", tlchi);
                                        p[9].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[10] = new SqlParameter("@NhanVien", ex.GetGuid(drnhanvien.SelectedValue));
                                        p[10].SqlDbType = SqlDbType.UniqueIdentifier;
                                        ex.ExecuteNonQuerry("sp_XDAPhieuChiCongNoThu_Add", p);


                                        p = new SqlParameter[6];
                                        p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                                        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[1] = new SqlParameter("@IDThanhToan", idthanhtoan);
                                        p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[2] = new SqlParameter("@IDPhieuChi", ex.GetGuid(drphieuchiton.SelectedValue));
                                        p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[3] = new SqlParameter("@SoTien", double.Parse(txtsotienhoanlai.Text.Trim().Equals("") ? "0" : txtsotienhoanlai.Text.Trim()));
                                        p[3].SqlDbType = SqlDbType.Float;
                                        p[4] = new SqlParameter("@LoaiTon", 2);
                                        p[4].SqlDbType = SqlDbType.Int;
                                        p[5] = new SqlParameter("@Del", 1);
                                        p[5].SqlDbType = SqlDbType.Int;
                                        ex.ExecuteNonQuerry("sp_xdaphieuchiton_Add", p);
                                    }
                                }
                                for (int i = 0; i < GV1.Rows.Count; i++)
                                {
                                    Guid phieuchi = Guid.NewGuid();
                                    Guid idtt = Guid.NewGuid();
                                    Guid nhapkho = Guid.NewGuid();
                                    TextBox txtnoidung = (TextBox)GV1.Rows[i].Cells[1].FindControl("txtnoidung");
                                    DropDownList dlhangmuc = (DropDownList)GV1.Rows[i].Cells[2].FindControl("dlhangmuc");
                                    DropDownList dldoituong = (DropDownList)GV1.Rows[i].Cells[3].FindControl("dldoituong");
                                    DropDownList dlnhacungcap = (DropDownList)GV1.Rows[i].Cells[4].FindControl("drnhacungcap");
                                    DropDownList dlnhomvatlieu = (DropDownList)GV1.Rows[i].Cells[5].FindControl("drnhomvatlieu");
                                    DropDownList dlloaivatlieu = (DropDownList)GV1.Rows[i].Cells[6].FindControl("drloaivatlieu");
                                    DropDownList dldonvi = (DropDownList)GV1.Rows[i].Cells[7].FindControl("dldonvi");
                                    TextBox txtsoluong = (TextBox)GV1.Rows[i].Cells[8].FindControl("txtsoluong");
                                    TextBox txtdongia = (TextBox)GV1.Rows[i].Cells[9].FindControl("txtdongia");
                                    TextBox txtthanhtienhang = (TextBox)GV1.Rows[i].Cells[10].FindControl("txtthanhtienhang");
                                    TextBox txtthanhtien = (TextBox)GV1.Rows[i].Cells[11].FindControl("txtthanhtien");
                                    TextBox txtmathietbi = (TextBox)GV1.Rows[i].Cells[12].FindControl("txtmathietbi");
                                    CheckBox ckVAT = (CheckBox)GV1.Rows[i].Cells[13].FindControl("ckVAT");

                                    CheckBox cknhapkho = (CheckBox)GV1.Rows[i].Cells[14].FindControl("cknhapkho");
                                    if (cknhapkho.Checked == true)
                                    {

                                        # region insert nhapkho
                                        p = new SqlParameter[15];
                                        p[0] = new SqlParameter("@IDs", nhapkho);
                                        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[1] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                                        p[1].SqlDbType = SqlDbType.SmallDateTime;
                                        p[2] = new SqlParameter("@NhaCungCap", ex.GetGuid(dlnhacungcap.SelectedValue));
                                        p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[3] = new SqlParameter("@NguoiMua", ex.GetGuid(drnhanvien.SelectedValue));
                                        p[3].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[4] = new SqlParameter("@NhomVatLieu", ex.GetGuid(dlnhomvatlieu.SelectedValue));
                                        p[4].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[5] = new SqlParameter("@LoaiVatLieu", ex.GetGuid(dlloaivatlieu.SelectedValue));
                                        p[5].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[6] = new SqlParameter("@DonVi", ex.GetGuid(dldonvi.SelectedValue));
                                        p[6].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[7] = new SqlParameter("@SoLuong", double.Parse(txtsoluong.Text.Trim().Equals("") ? "0" : txtsoluong.Text.Trim()));
                                        p[7].SqlDbType = SqlDbType.Float;
                                        p[8] = new SqlParameter("@DonGia", double.Parse(txtdongia.Text.Trim().Equals("") ? "0" : txtdongia.Text.Trim()));
                                        p[8].SqlDbType = SqlDbType.Float;
                                        p[9] = new SqlParameter("@ThanhTien", double.Parse(txtthanhtien.Text.Trim().Equals("") ? "0" : txtthanhtien.Text.Trim()));
                                        p[9].SqlDbType = SqlDbType.Float;
                                        p[10] = new SqlParameter("@MaThietBi", txtmathietbi.Text.Trim());
                                        p[10].SqlDbType = SqlDbType.NVarChar;
                                        p[11] = new SqlParameter("@Del", 1);
                                        p[11].SqlDbType = SqlDbType.Int;
                                        p[12] = new SqlParameter("@CoVAT", ckVAT.Checked ? 1 : 2);
                                        p[12].SqlDbType = SqlDbType.Int;
                                        p[13] = new SqlParameter("@IDThanhToan", idthanhtoan);
                                        p[13].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[14] = new SqlParameter("@ThanhTienHang", double.Parse(txtthanhtienhang.Text.Trim().Equals("") ? "0" : txtthanhtienhang.Text.Trim()));
                                        p[14].SqlDbType = SqlDbType.Float;
                                        ex.ExecuteNonQuerry("sp_xdanhapkho_Add", p);
                                        #endregion
                                        #region


                                        p = new SqlParameter[1];
                                        p[0] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                                        p[0].SqlDbType = SqlDbType.SmallDateTime;
                                        dt = ex.GetData("sp_xdagetsophieuchiconnotravat_TienMat", p);
                                        int sopc = Int16.Parse(dt.Rows[0]["sopc"].ToString());
                                        if (sopc == 0)
                                            pc = 1;
                                        else pc = sopc + 1;

                                        p = new SqlParameter[21];
                                        p[0] = new SqlParameter("@IDs", idtt);
                                        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[1] = new SqlParameter("@NhaCungCap", ex.GetGuid(dlnhacungcap.SelectedValue));
                                        p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[2] = new SqlParameter("@SoDuKiTruocVAT", double.Parse(0.ToString()));
                                        p[2].SqlDbType = SqlDbType.Float;

                                        p[3] = new SqlParameter("@TienCuocVAT", double.Parse(0.ToString()));
                                        p[3].SqlDbType = SqlDbType.Float;
                                        p[4] = new SqlParameter("@SoDuKiTruocKhongVAT", double.Parse(0.ToString()));
                                        p[4].SqlDbType = SqlDbType.Float;

                                        p[5] = new SqlParameter("@TienCuocKhongVAT", double.Parse(0.ToString()));
                                        p[5].SqlDbType = SqlDbType.Float;
                                        p[6] = new SqlParameter("@SoTienThanhToanTruocVAT", double.Parse(0.ToString()));
                                        p[6].SqlDbType = SqlDbType.Float;
                                        p[7] = new SqlParameter("@SoTienThanhToanKhongVAT", double.Parse(0.ToString()));
                                        p[7].SqlDbType = SqlDbType.Float;
                                        if (ckVAT.Checked == true)
                                        {
                                            p[8] = new SqlParameter("@TienHangVAT", double.Parse(txtthanhtienhang.Text.Trim().Equals("") ? "0" : txtthanhtienhang.Text.Trim()));
                                            p[8].SqlDbType = SqlDbType.Float;
                                            p[9] = new SqlParameter("@TienHangKhongVAT", double.Parse(0.ToString()));
                                            p[9].SqlDbType = SqlDbType.Float;
                                            p[10] = new SqlParameter("@SoTienThanhToanKiNayVAT", double.Parse(txtthanhtien.Text.Trim().Equals("") ? "0" : txtthanhtien.Text.Trim()));
                                            p[10].SqlDbType = SqlDbType.Float;
                                            p[11] = new SqlParameter("@SoTienThanhToanKiNayKhongVAT", double.Parse(0.ToString()));
                                            p[11].SqlDbType = SqlDbType.Float;
                                        }
                                        else
                                        {
                                            p[8] = new SqlParameter("@TienHangVAT", double.Parse(0.ToString()));
                                            p[8].SqlDbType = SqlDbType.Float;
                                            p[9] = new SqlParameter("@TienHangKhongVAT", double.Parse(txtthanhtienhang.Text.Trim().Equals("") ? "0" : txtthanhtienhang.Text.Trim()));
                                            p[9].SqlDbType = SqlDbType.Float;
                                            p[10] = new SqlParameter("@SoTienThanhToanKiNayVAT", double.Parse(0.ToString()));
                                            p[10].SqlDbType = SqlDbType.Float;
                                            p[11] = new SqlParameter("@SoTienThanhToanKiNayKhongVAT", double.Parse(txtthanhtien.Text.Trim().Equals("") ? "0" : txtthanhtien.Text.Trim()));
                                            p[11].SqlDbType = SqlDbType.Float;
                                        }
                                        p[12] = new SqlParameter("@HinhThucThanhToanVAT", 2);
                                        p[12].SqlDbType = SqlDbType.Int;
                                        p[13] = new SqlParameter("@HinhThucThanhToanKhongVAT", double.Parse(0.ToString()));
                                        p[13].SqlDbType = SqlDbType.Int;
                                        p[14] = new SqlParameter("@SoPC", pc);
                                        p[14].SqlDbType = SqlDbType.Int;
                                        p[15] = new SqlParameter("@TaiKhoanKhongVAT", DBNull.Value);
                                        p[15].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[16] = new SqlParameter("@TaiKhoanVAT", DBNull.Value);
                                        p[16].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[17] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                                        p[17].SqlDbType = SqlDbType.SmallDateTime;
                                        p[18] = new SqlParameter("@Del", 1);
                                        p[18].SqlDbType = SqlDbType.Int;
                                        p[19] = new SqlParameter("@NoiDung", txtnoidung.Text.Trim());
                                        p[19].SqlDbType = SqlDbType.NVarChar;
                                        p[20] = new SqlParameter("@IDLenhChi", idthanhtoan);
                                        p[20].SqlDbType = SqlDbType.UniqueIdentifier;
                                        ex.ExecuteNonQuerry("sp_XDAThanhToanCongNoTra_Add", p);

                                        p = new SqlParameter[11];
                                        p[0] = new SqlParameter("@IDs", phieuchi);
                                        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[1] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                                        p[1].SqlDbType = SqlDbType.SmallDateTime;
                                        p[2] = new SqlParameter("@NoiDung", txtnoidung.Text.Trim());
                                        p[2].SqlDbType = SqlDbType.NVarChar;
                                        p[3] = new SqlParameter("@DonVi", ex.GetGuid(dlnhacungcap.SelectedValue));
                                        p[3].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[4] = new SqlParameter("@SoPC", pc);
                                        p[4].SqlDbType = SqlDbType.Int;
                                        p[5] = new SqlParameter("@TaiKhoan", DBNull.Value);
                                        p[5].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[6] = new SqlParameter("@Del", 1);
                                        p[6].SqlDbType = SqlDbType.Int;
                                        p[7] = new SqlParameter("@LoaiCongNo", 2);
                                        p[7].SqlDbType = SqlDbType.Int;
                                        p[8] = new SqlParameter("@SoTien", double.Parse(txtthanhtien.Text.Trim().Equals("") ? "0" : txtthanhtien.Text.Trim()));
                                        p[8].SqlDbType = SqlDbType.Float;
                                        p[9] = new SqlParameter("@IDLenhChi", idtt);
                                        p[9].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[10] = new SqlParameter("@NhanVien", ex.GetGuid(drnhanvien.SelectedValue));
                                        p[10].SqlDbType = SqlDbType.UniqueIdentifier;
                                        ex.ExecuteNonQuerry("sp_xdaphieuchicongno_Add", p);

                                        #endregion}

                                    }
                                }

                                btnCancel_Click(sender, e);
                                gstGetMess("Lưu thành công", "");
                            }
                    }
                }
            }
            catch (Exception ax)
            {
                gstGetMess("Có lỗi trong quá trình lưu, xem trong file log", "");
                ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tThiết lập thanh toán tiền mặt\t" + ax.Message);
            }
        }
        else
            gstGetMess("Bạn không có quyền thêm dữ liệu trên trang này", "");
    }
    protected void txtsotienhoanlai_TextChanged(object sender, EventArgs e)
    {

    }
    protected void ckhoantheophieuchi_CheckedChanged(object sender, EventArgs e)
    {
        txtsotienhoanlai.Visible = true;

    }
    protected void btnFirst_Click(object sender, EventArgs e)
    {
        CurrentPage = 1;
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", CurrentPage);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;

        GV.DataSource = ex.GetData("sp_xdaThietLapPhieuChi_List", p);
        GV.DataBind();

        btnPre.Enabled = false;
        btnNext.Enabled = true;
        btnFirst.Enabled = false;
    }
    protected void btnPre_Click(object sender, EventArgs e)
    {
        if (CurrentPage > 1)
        {
            CurrentPage--;
            btnPre.Enabled = true;
            btnNext.Enabled = true;
            btnFirst.Enabled = true;

            p = new SqlParameter[2];
            p[0] = new SqlParameter("@PageNumber", CurrentPage);
            p[0].SqlDbType = SqlDbType.Int;
            p[1] = new SqlParameter("@RowspPage", PageNumber);
            p[1].SqlDbType = SqlDbType.Int;

            GV.DataSource = ex.GetData("sp_xdaThietLapPhieuChi_List", p);
            GV.DataBind();

        }
        else
        {
            btnFirst.Enabled = false;
            btnPre.Enabled = false;
            btnNext.Enabled = true;
        }
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        CurrentPage++;
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", CurrentPage);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;

        GV.DataSource = ex.GetData("sp_xdaThietLapPhieuChi_List", p);
        GV.DataBind();

        if (GV.Rows.Count.Equals(0))
        {
            btnPre.Enabled = true;
            btnNext.Enabled = false;
        }
        else
        {
            btnPre.Enabled = true;
            btnNext.Enabled = true;
        }
        btnFirst.Enabled = true;
    }
    public static void MergeRows(GridView gridView)
    {
        for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--)
        {
            GridViewRow row = gridView.Rows[rowIndex];
            GridViewRow previousRow = gridView.Rows[rowIndex + 1];

            for (int i = 0; i < row.Cells.Count; i++)
            {
                if (row.Cells[i].Text.Equals(previousRow.Cells[i].Text))
                {
                    row.Cells[i].RowSpan = previousRow.Cells[i].RowSpan < 2 ? 2 : previousRow.Cells[i].RowSpan + 1;
                    previousRow.Cells[i].Visible = false;
                }
            }
        }
    }
    protected void GV_PreRender(object sender, EventArgs e)
    {
        MergeRows(GV);
    }
    protected void dlThang_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (dlThang.SelectedIndex.Equals(0))
        {
            txtTuNgay.Visible = true;
            txtDenNgay.Visible = true;
            dlNam.Visible = false;
        }
        else
        {
            txtTuNgay.Visible = false;
            txtDenNgay.Visible = false;
            dlNam.Visible = true;
        }
    }
    protected void btnXem_Click(object sender, EventArgs e)
    {
        getDate();
        if (dropNhanVien.SelectedIndex == 0)
        {
            p = new SqlParameter[2];
            p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
            p[0].SqlDbType = SqlDbType.SmallDateTime;
            p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
            p[1].SqlDbType = SqlDbType.SmallDateTime;
            GV.DataSource = ex.GetData("sp_xdaThietLapPhieuChi_ListTheoThoiGian", p);
            GV.DataBind();
            p = new SqlParameter[2];
            p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
            p[0].SqlDbType = SqlDbType.SmallDateTime;
            p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
            p[1].SqlDbType = SqlDbType.SmallDateTime;
            dt = ex.GetData("sp_xdaThietLapPhieuChi_ListTheoThoiGian", p);
            LoadFooter();
            p = new SqlParameter[2];
            p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
            p[0].SqlDbType = SqlDbType.SmallDateTime;
            p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
            p[1].SqlDbType = SqlDbType.SmallDateTime;
            dtPrint = ex.GetData("sp_xdaThietLapPhieuChi_ListTheoThoiGian_Print", p);
        }
        else
        {
            p = new SqlParameter[3];
            p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
            p[0].SqlDbType = SqlDbType.SmallDateTime;
            p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
            p[1].SqlDbType = SqlDbType.SmallDateTime;


            p[2] = new SqlParameter("@NhanVien", ex.GetGuid(dropNhanVien.SelectedValue));
            p[2].SqlDbType = SqlDbType.UniqueIdentifier;
            GV.DataSource = ex.GetData("sp_xdaThietLapPhieuChi_ListNhanVien", p);
            GV.DataBind();

            p = new SqlParameter[3];
            p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
            p[0].SqlDbType = SqlDbType.SmallDateTime;
            p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
            p[1].SqlDbType = SqlDbType.SmallDateTime;


            p[2] = new SqlParameter("@NhanVien", ex.GetGuid(dropNhanVien.SelectedValue));
            p[2].SqlDbType = SqlDbType.UniqueIdentifier;
            dt= ex.GetData("sp_xdaThietLapPhieuChi_ListNhanVien", p);
            LoadFooter();
        }
        btnFirst.Visible = false;
        btnPre.Visible = false; btnNext.Visible = false;

    }
    public void getDate()
    {
        if (dlThang.SelectedIndex.Equals(0))
        {
            hdTuNgay.Value = txtTuNgay.Text;
            hdDenNgay.Value = txtDenNgay.Text;
        }
        else
        {
            txtTuNgay.Text = "1/" + dlThang.SelectedValue + "/" + dlNam.SelectedValue;
            txtDenNgay.Text = GetLastDayOfMonth(int.Parse(dlThang.SelectedValue)).ToString("dd/MM/yyyy");
            hdTuNgay.Value = txtTuNgay.Text;
            hdDenNgay.Value = txtDenNgay.Text;
        }
    }
    public DateTime GetLastDayOfMonth(int iMonth)
    {
        DateTime dtResult = new DateTime(int.Parse(dlNam.SelectedValue), iMonth, 1);
        dtResult = dtResult.AddMonths(1);
        dtResult = dtResult.AddDays(-(dtResult.Day));
        return dtResult;
    }
    public void getDateDoiTuong()
    {

    }
    public DateTime GetLastDayOfMonthDoituong(int iMonth)
    {
        DateTime dtResult = new DateTime(int.Parse(nam.SelectedValue), iMonth, 1);
        dtResult = dtResult.AddMonths(1);
        dtResult = dtResult.AddDays(-(dtResult.Day));
        return dtResult;
    }
    protected void GV_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string ID = e.CommandArgument.ToString();
        hdID.Value = ID;
        Guid gID = new Guid(ID);
        p = new SqlParameter[1];


        if (e.CommandName.Equals("Del"))
        {
            p[0] = new SqlParameter("@IDs", gID);
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
            ex.ExecuteNonQuerry("sp_TLThanhToan_DelPhieuChiCongNo", p);
            p[0] = new SqlParameter("@IDs", gID);
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
            ex.ExecuteNonQuerry("sp_TLThanhToan_DelPhieuChiCongNoThu", p);
            p[0] = new SqlParameter("@IDs", gID);
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
            ex.ExecuteNonQuerry("sp_TLThanhToan_DelThietLapPCTienMat", p);
            p[0] = new SqlParameter("@IDs", gID);
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
            ex.ExecuteNonQuerry("sp_TLThanhToan_DelThietLapPTHuTienMat", p);
            p[0] = new SqlParameter("@IDs", gID);
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
            ex.ExecuteNonQuerry("Del_XDANoiDungTTTienMat", p);
            p[0] = new SqlParameter("@IDs", gID);
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
            ex.ExecuteNonQuerry("sp_TLThanhToanTienMat_DelNhapKho", p);
            p[0] = new SqlParameter("@IDs", gID);
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
            ex.ExecuteNonQuerry("sp_TLThanhToanTienMat_DelPhieuChiCongNo", p);
            p[0] = new SqlParameter("@IDs", gID);
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
            ex.ExecuteNonQuerry("sp_TLThanhToanTienMat_DelThanhToanCongNoTra", p);
            p[0] = new SqlParameter("@IDs", gID);
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
            ex.ExecuteNonQuerry("Del_XDAChiTietThanhToan", p);
            p[0] = new SqlParameter("@IDs", gID);
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
            ex.ExecuteNonQuerry("Del_XDAPhieuChiTon", p);
            p[0] = new SqlParameter("@IDs", gID);
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
            ex.ExecuteNonQuerry("sp_TLThanhToan_DelThanhToan", p);


            gstGetMess("Xóa thành công", "");
            btnCancel_Click(sender, e);
        }
        else if (e.CommandName.Equals("chitiet"))
        {
            p = new SqlParameter[1];

            p[0] = new SqlParameter("@IDs", gID);
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
            dt = ex.GetData("sp_xdaThietLapPhieuChi_ChiTietPhieuChi", p);
            if (dt.Rows.Count > 0)
            {
                pcngaythang.Text = "  " + dt.Rows[0]["ngaythang"].ToString();
                pchoten.Text = dt.Rows[0]["hoten"].ToString();
                pcbophan.Text = dt.Rows[0]["tenbophan"].ToString();
                pcchucvu.Text = dt.Rows[0]["tenchucvu"].ToString();
                pctt.Text = dt.Rows[0]["sophieutt"].ToString();
                pcpt.Text = dt.Rows[0]["sophieuthu"].ToString();
                pcsophieuchi.Text = dt.Rows[0]["sopc"].ToString();
                lbvietbangchu.Text = "(" + ToString(decimal.Parse(dt.Rows[0]["sotien"].ToString())) + ")";
            }
            p[0] = new SqlParameter("@IDs", gID);
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
            GridView1.DataSource = ex.GetData("sp_xdaThietLapPhieuChi_ChiTietPhieuChi_NoiDung", p);
            GridView1.DataBind();
            GridView1.FooterRow.Cells[0].Text = "Tổng cộng";
            GridView1.FooterRow.Cells[0].HorizontalAlign = HorizontalAlign.Center;

            GridView1.FooterRow.Cells[2].Text = ConvertDecimal(double.Parse(dt.Rows[0]["sotien"].ToString()));
            GridView1.FooterRow.Cells[2].HorizontalAlign = HorizontalAlign.Right;
            md.Show();
        }
        else if (e.CommandName.Equals("chitietphieuthu"))
        {
            p[0] = new SqlParameter("@IDs", gID);
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
            dt = ex.GetData("sp_xdaThietLapPhieuChi_ChiTietPhieuThu", p);
            if (dt.Rows.Count > 0)
            {
                thungaythang.Text = dt.Rows[0]["ngaythang"].ToString();
                thuhoten.Text = dt.Rows[0]["hoten"].ToString();
                thubophan.Text = dt.Rows[0]["tenbophan"].ToString();
                thuchucvu.Text = dt.Rows[0]["tenchucvu"].ToString();

                thusophieu.Text = dt.Rows[0]["sopc"].ToString();
                thusopc.Text = dt.Rows[0]["sophieuchi"].ToString();
                thusott.Text = dt.Rows[0]["SoPhieuTT"].ToString();
                thuvietbangchu.Text = "(" + ToString(decimal.Parse(dt.Rows[0]["sotien"].ToString())) + ")";
            }
            p[0] = new SqlParameter("@IDs", gID);
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
            GridView3.DataSource = ex.GetData("sp_xdaThietLapThu_ChiTietPhieuThu_NoiDung", p);
            GridView3.DataBind();
            GridView3.FooterRow.Cells[0].Text = "Tổng cộng";
            GridView3.FooterRow.Cells[0].HorizontalAlign = HorizontalAlign.Center;

            GridView3.FooterRow.Cells[2].Text = ConvertDecimal(double.Parse(dt.Rows[0]["sotien"].ToString()));
            GridView3.FooterRow.Cells[2].HorizontalAlign = HorizontalAlign.Right;
            ModalPopupExtender1.Show();
        }
        else if (e.CommandName.Equals("chitietthanhtoan"))
        {
            p[0] = new SqlParameter("@IDs", gID);
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
            dt = ex.GetData("[sp_xdaChiTietThanhToan_ThôngTinNV]", p);
            if (dt.Rows.Count > 0)
            {
                ttngaythang.Text = dt.Rows[0]["ngaythang"].ToString();
                tthoten.Text = dt.Rows[0]["hoten"].ToString();
                ttbophan.Text = dt.Rows[0]["tenbophan"].ToString();
                ttchucvu.Text = dt.Rows[0]["tenchucvu"].ToString();
                ttsophieu.Text = dt.Rows[0]["sophieutt"].ToString();
                lbphieuthu.Text = dt.Rows[0]["sopt"].ToString();
            }
            p[0] = new SqlParameter("@IDs", gID);
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
            GridView2.DataSource = ex.GetData("sp_ChiTietThanhToan_List_NoiDung", p);
            GridView2.DataBind();
            GridView2.FooterRow.Cells[1].Text = "Tổng cộng";
            GridView2.FooterRow.Cells[1].HorizontalAlign = HorizontalAlign.Center;
            double s1 = 0, s2 = 0, s3 = 0; ;
            for (int i = 0; i < GridView2.Rows.Count; i++)
            {
               
                s2 += double.Parse(GridView2.Rows[i].Cells[8].Text.Trim().Replace(",", ""));
             

            }
            for (int i = 0; i < GridView2.Rows.Count; i++)
            {
                s1 += double.Parse(GridView2.Rows[i].Cells[7].Text.Trim().Replace(",", ""));
               
             

            }
            s3 = s3 + s1 + s2;
            GridView2.FooterRow.Cells[7].Text = s3 == 0 ? "0" : string.Format("{0:0,0}", s3);
            GridView2.FooterRow.Cells[7].HorizontalAlign = HorizontalAlign.Center;
            Label1.Text = "(" + ToString(decimal.Parse(s3.ToString())) + ")";
            p[0] = new SqlParameter("@IDs", gID);
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
            dt = ex.GetData("sp_ChiTietThanhToan", p);
            ltlFirst.Text = "";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ltlFirst.Text += dt.Rows[i]["sopc"].ToString() + ",";
            }
            ltlFirst.Text = ltlFirst.Text.Substring(0, ltlFirst.Text.Length - 1);
            chitietthanhtoan.Show();
        }
        else if (e.CommandName.Equals("Select"))
        {
            #region suathanhtoan
            if (f_Sua.Equals(1))
            {

                p = new SqlParameter[1];
                p[0] = new SqlParameter("@IDs", gID);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                ex.ExecuteNonQuerry("sp_TLThanhToanTienMat_DelNhapKho", p);
                p = new SqlParameter[1];
                p[0] = new SqlParameter("@IDs", gID);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                ex.ExecuteNonQuerry("sp_TLThanhToanTienMat_DelPhieuChiCongNo", p);
                p = new SqlParameter[1];
                p[0] = new SqlParameter("@IDs", gID);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                ex.ExecuteNonQuerry("sp_TLThanhToanTienMat_DelThanhToanCongNoTra", p);

                int rowIndex = 0;
                p = new SqlParameter[1];
                p[0] = new SqlParameter("@IDs", gID);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                dt = ex.GetData("sp_xdanoidungtttienmat_List_ChiTietByID", p);
                if (ViewState["CurrentTable"] != null)
                {

                    if (dt.Rows.Count > 0)
                    {
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            TextBox txtnoidung = (TextBox)GV1.Rows[rowIndex].Cells[1].FindControl("txtnoidung");
                            DropDownList dlhangmuc = (DropDownList)GV1.Rows[rowIndex].Cells[2].FindControl("dlhangmuc");
                            DropDownList dldoituong = (DropDownList)GV1.Rows[rowIndex].Cells[3].FindControl("dldoituong");
                            DropDownList dlnhacungcap = (DropDownList)GV1.Rows[rowIndex].Cells[4].FindControl("drnhacungcap");
                            DropDownList dlnhomvatlieu = (DropDownList)GV1.Rows[rowIndex].Cells[5].FindControl("drnhomvatlieu");
                            DropDownList dlloaivatlieu = (DropDownList)GV1.Rows[rowIndex].Cells[6].FindControl("drloaivatlieu");
                            DropDownList dldonvi = (DropDownList)GV1.Rows[rowIndex].Cells[7].FindControl("dldonvi");
                            TextBox txtsoluong = (TextBox)GV1.Rows[rowIndex].Cells[8].FindControl("txtsoluong");
                            TextBox txtdongia = (TextBox)GV1.Rows[rowIndex].Cells[9].FindControl("txtdongia");
                            TextBox txtthanhtienhang = (TextBox)GV1.Rows[rowIndex].Cells[10].FindControl("txtthanhtienhang");
                            TextBox txtthanhtien = (TextBox)GV1.Rows[rowIndex].Cells[11].FindControl("txtthanhtien");
                            TextBox txtmathietbi = (TextBox)GV1.Rows[rowIndex].Cells[12].FindControl("txtmathietbi");
                            CheckBox ckVAT = (CheckBox)GV1.Rows[rowIndex].Cells[13].FindControl("ckVAT");

                            CheckBox cknhapkho = (CheckBox)GV1.Rows[rowIndex].Cells[14].FindControl("cknhapkho");
                            CheckBox cktinhchochiha = (CheckBox)GV1.Rows[rowIndex].Cells[15].FindControl("cktinhchochiha");
                            CheckBox cktaisancodinh = (CheckBox)GV1.Rows[rowIndex].Cells[16].FindControl("cktaisancodinh");
                            GV1.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
                            txtnoidung.Text = dt.Rows[i]["Noidung"].ToString();
                            if (dt.Rows[i]["tenhangmucchi"].ToString() != "")
                            {
                                dlhangmuc.SelectedValue = dt.Rows[i]["tenhangmucchi"].ToString();
                            }
                            if (dt.Rows[i]["doituongchi"].ToString() != "")
                            {
                                dldoituong.SelectedValue = dt.Rows[i]["doituongchi"].ToString();
                            }
                            if (dt.Rows[i]["TenNCC"].ToString() != "")
                            {
                                dlnhacungcap.SelectedValue = dt.Rows[i]["TenNCC"].ToString();
                            }
                            if (dt.Rows[i]["TenNhomVatTu"].ToString() != "")
                            {
                                dlnhomvatlieu.SelectedValue = dt.Rows[i]["TenNhomVatTu"].ToString();
                            }
                            if (dt.Rows[i]["TenLoaiVL"].ToString() != "")
                            {
                                dlloaivatlieu.SelectedValue = dt.Rows[i]["TenLoaiVL"].ToString();
                            }
                            if (dt.Rows[i]["donvi"].ToString() != "")
                            {
                                dldonvi.SelectedValue = dt.Rows[i]["donvi"].ToString();
                            }
                            if (dt.Rows[i]["soluong"].ToString() != "")
                            {
                                txtsoluong.Text = dt.Rows[i]["soluong"].ToString();
                            }

                            txtdongia.Text = dt.Rows[i]["dongia"].ToString();
                            txtmathietbi.Text = dt.Rows[i]["mathietbi"].ToString();
                            txtthanhtien.Text = dt.Rows[i]["thanhtien"].ToString();
                            txtthanhtienhang.Text = dt.Rows[i]["thanhtienhang"].ToString();
                            string vat = dt.Rows[i]["covat"].ToString();
                            string tinhchochiha = dt.Rows[i]["TinhChoChiHa"].ToString();
                            string nhapkho = dt.Rows[i]["NhapKho"].ToString();
                            string taisancodinh = dt.Rows[i]["Taisancodinh"].ToString();
                            if (vat.Equals("1")) ckVAT.Checked = true;
                            else ckVAT.Checked = false;
                            if (tinhchochiha.Equals("1")) cktinhchochiha.Checked = true;
                            else cktinhchochiha.Checked = false;
                            if (nhapkho.Equals("1")) cknhapkho.Checked = true;
                            else cknhapkho.Checked = false;
                            if (taisancodinh.Equals("1")) cktaisancodinh.Checked = true;
                            else cktaisancodinh.Checked = false;
                            rowIndex++;
                            if (txtnoidung.Text != null)
                            {
                                AddNewRow();
                            }
                        }
                    }

                }

                else
                {
                    Response.Write("ViewState is null");
                }


                txtngaythang.Text = dt.Rows[0]["ngaythang"].ToString();
                drnhanvien.SelectedValue = dt.Rows[0]["nhanvien"].ToString();
                drnhanvien_SelectedIndexChanged(sender, e);

                p = new SqlParameter[1];
                p[0] = new SqlParameter("@IDs", gID);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;

                dt = ex.GetData("sp_TLThanhToanTienMat_ListPhieuChi_UpDate", p);
                if (dt.Rows.Count > 0)
                {
                    drphieuchi.DataSource = dt;
                    drphieuchi.DataTextField = "sopc";
                    drphieuchi.DataValueField = "idlenhchi";
                    drphieuchi.DataBind();
                }
                p = new SqlParameter[1];
                p[0] = new SqlParameter("@IDs", gID);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                drphieuchiton.Items.Clear();
                drphieuchiton.DataSource = ex.GetData("sp_TLThanhToanTienMat_ListPhieuChi_UpDate", p);
                drphieuchiton.DataBind();
                drphieuchiton.Items.Insert(0, new ListItem("--Chọn phiếu chi tồn--", ""));
                drphieuchiton.SelectedIndex = 0;

                p = new SqlParameter[1];
                p[0] = new SqlParameter("@IDs", gID);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;

                dt = ex.GetData("sp_xdathanhtoantienmat_SoTienTheoThanhToan", p);
                if (dt.Rows.Count > 0)
                {
                    tongtienh.Text = dt.Rows[0]["sotien"].ToString();
                }
                p = new SqlParameter[1];
                p[0] = new SqlParameter("@IDs", gID);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                ex.ExecuteNonQuerry("Del_XDAChiTietThanhToan", p);
                p = new SqlParameter[1];
                p[0] = new SqlParameter("@IDs", gID);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                ex.ExecuteNonQuerry("Del_XDAPhieuChiTon", p);


                btncapnhat.Visible = true;
                btnSave.Visible = false;
            #endregion

            }
        }

        else if (e.CommandName.Equals("xemchitiet"))
        {
            #region suathanhtoan
            if (f_Sua.Equals(1))
            {

                p = new SqlParameter[1];
                p[0] = new SqlParameter("@IDs", gID);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;

                dt = ex.GetData("[sp_xdanoidungtttienmat_List_ChiTiet]", p);
                GridView4.DataSource = dt;
                GridView4.DataBind();
                ModalPopupExtender2.Show();
            #endregion

            }
        }
    }

    protected void txtchucvu_TextChanged(object sender, EventArgs e)
    {

    }
    protected void thanhtienhang_TextChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < GV1.Rows.Count; i++)
        {

            TextBox txtdongia = (TextBox)GV1.Rows[i].Cells[1].FindControl("txtdongia");
            TextBox txtsoluong = (TextBox)GV1.Rows[i].Cells[2].FindControl("txtsoluong");
            TextBox txtthanhtien = (TextBox)GV1.Rows[i].Cells[3].FindControl("txtthanhtienhang");
            double thanhtien;
            thanhtien = double.Parse(txtdongia.Text.Trim()) * double.Parse(txtsoluong.Text.Trim());
            txtthanhtien.Text = thanhtien.ToString();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        CurrentPage = 1;
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", CurrentPage);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;

        GVDoiTuong.DataSource = ex.GetData("sp_XDANoiDungTTTienMat_List", p);
        GVDoiTuong.DataBind();

        Button2.Enabled = false;
        Button3.Enabled = true;
        Button1.Enabled = false;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (CurrentPage > 1)
        {
            CurrentPage--;
            Button2.Enabled = true;
            Button3.Enabled = true;
            Button1.Enabled = true;

            p = new SqlParameter[2];
            p[0] = new SqlParameter("@PageNumber", CurrentPage);
            p[0].SqlDbType = SqlDbType.Int;
            p[1] = new SqlParameter("@RowspPage", PageNumber);
            p[1].SqlDbType = SqlDbType.Int;

            GVDoiTuong.DataSource = ex.GetData("sp_XDANoiDungTTTienMat_List", p);
            GVDoiTuong.DataBind();

        }
        else
        {
            Button1.Enabled = false;
            Button2.Enabled = false;
            Button3.Enabled = true;
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        CurrentPage++;
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", CurrentPage);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;

        GVDoiTuong.DataSource = ex.GetData("sp_XDANoiDungTTTienMat_List", p);
        GVDoiTuong.DataBind();

        if (GV.Rows.Count.Equals(0))
        {
            Button2.Enabled = true;
            Button3.Enabled = false;
        }
        else
        {
            Button2.Enabled = true;
            Button3.Enabled = true;
        }
        Button1.Enabled = true;
    }
    protected void thang_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void btnXemDT_Click(object sender, EventArgs e)
    {
        getDate();
        if (dldoituong.SelectedIndex == 0)
        {
            p = new SqlParameter[2];
            p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
            p[0].SqlDbType = SqlDbType.SmallDateTime;
            p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
            p[1].SqlDbType = SqlDbType.SmallDateTime;
            GVDoiTuong.DataSource = ex.GetData("sp_XDANoiDungTTTienMat_List_TheoThoiGian", p);
            GVDoiTuong.DataBind();
            p = new SqlParameter[2];
            p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
            p[0].SqlDbType = SqlDbType.SmallDateTime;
            p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
            p[1].SqlDbType = SqlDbType.SmallDateTime;
            dt = ex.GetData("sp_XDANoiDungTTTienMat_List_TheoThoiGian", p);
            LoadFooter1();
        }
        else
        {
            p = new SqlParameter[3];
            p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
            p[0].SqlDbType = SqlDbType.SmallDateTime;
            p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
            p[1].SqlDbType = SqlDbType.SmallDateTime;
            p[2] = new SqlParameter("@DoiTuong", ex.GetGuid(dldoituong.SelectedValue));
            p[2].SqlDbType = SqlDbType.UniqueIdentifier;
            GVDoiTuong.DataSource = ex.GetData("sp_XDANoiDungTTTienMat_List_TheoThoiGian_DoiTuong", p);
            GVDoiTuong.DataBind();
            p = new SqlParameter[3];
            p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
            p[0].SqlDbType = SqlDbType.SmallDateTime;
            p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
            p[1].SqlDbType = SqlDbType.SmallDateTime;
            p[2] = new SqlParameter("@DoiTuong", ex.GetGuid(dldoituong.SelectedValue));
            p[2].SqlDbType = SqlDbType.UniqueIdentifier;
            dt= ex.GetData("sp_XDANoiDungTTTienMat_List_TheoThoiGian_DoiTuong", p);
            LoadFooter1();
        }
        Button1.Visible = false;
        Button2.Visible = false; Button3.Visible = false;
    }
    protected void btncapnhat_Click(object sender, EventArgs e)
    {

        if (f_Sua.Equals(1))
        {
            try
            {
                Guid gID1 = new Guid(hdID.Value);
                p = new SqlParameter[1];
                p[0] = new SqlParameter("@IDs", gID1);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                ex.ExecuteNonQuerry("Del_XDANoiDungTTTienMat", p);
                if (double.Parse(txtsotienton.Text.Trim()) != 0 && drphieuchiton.SelectedIndex == 0)
                    gstGetMess("Tồn tại số tiền tồn.Chọn phiếu chi tồn", "");
                else
                    if (double.Parse(txtsotienton.Text.Trim()) == 0 && drphieuchiton.SelectedIndex > 0)
                        gstGetMess("Không chọn phiếu tồn", "");
                    else
                    {

                        Guid gID = new Guid(hdID.Value);
                        p = new SqlParameter[6];
                        p[0] = new SqlParameter("@IDs", gID);
                        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                        p[1] = new SqlParameter("@NhanVien", ex.GetGuid(drnhanvien.SelectedValue));
                        p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                        p[2] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                        p[2].SqlDbType = SqlDbType.SmallDateTime;
                        p[3] = new SqlParameter("@IDPhieuChi", DBNull.Value);
                        p[3].SqlDbType = SqlDbType.UniqueIdentifier;
                        p[4] = new SqlParameter("@TonTheoPhieuChi", double.Parse(txtsotien.Text.Trim().Equals("") ? "0" : txtsotien.Text.Trim()));
                        p[4].SqlDbType = SqlDbType.Float;
                        p[5] = new SqlParameter("@Del", 1);
                        p[5].SqlDbType = SqlDbType.Int;
                        //p[6] = new SqlParameter("@SoPhieuTT", sopt);
                        //p[6].SqlDbType = SqlDbType.Int;
                        ex.ExecuteNonQuerry("sp_TLThanhToan_UpdateThanhToan", p);

                        for (int i = 0; i < GV1.Rows.Count; i++)
                        {

                            TextBox txtnoidung = (TextBox)GV1.Rows[i].Cells[1].FindControl("txtnoidung");
                            DropDownList dlhangmuc = (DropDownList)GV1.Rows[i].Cells[2].FindControl("dlhangmuc");
                            DropDownList dldoituong = (DropDownList)GV1.Rows[i].Cells[3].FindControl("dldoituong");
                            DropDownList dlnhacungcap = (DropDownList)GV1.Rows[i].Cells[4].FindControl("drnhacungcap");
                            DropDownList dlnhomvatlieu = (DropDownList)GV1.Rows[i].Cells[5].FindControl("drnhomvatlieu");
                            DropDownList dlloaivatlieu = (DropDownList)GV1.Rows[i].Cells[6].FindControl("drloaivatlieu");
                            DropDownList dldonvi = (DropDownList)GV1.Rows[i].Cells[7].FindControl("dldonvi");
                            TextBox txtsoluong = (TextBox)GV1.Rows[i].Cells[8].FindControl("txtsoluong");
                            TextBox txtdongia = (TextBox)GV1.Rows[i].Cells[9].FindControl("txtdongia");
                            TextBox txtthanhtienhang = (TextBox)GV1.Rows[i].Cells[10].FindControl("txtthanhtienhang");
                            TextBox txtthanhtien = (TextBox)GV1.Rows[i].Cells[11].FindControl("txtthanhtien");
                            TextBox txtmathietbi = (TextBox)GV1.Rows[i].Cells[12].FindControl("txtmathietbi");
                            CheckBox ckVAT = (CheckBox)GV1.Rows[i].Cells[13].FindControl("ckVAT");

                            CheckBox cknhapkho = (CheckBox)GV1.Rows[i].Cells[14].FindControl("cknhapkho");
                            CheckBox cktinhchochiha = (CheckBox)GV1.Rows[i].Cells[15].FindControl("cktinhchochiha");
                            CheckBox cktaisancodinh = (CheckBox)GV1.Rows[i].Cells[15].FindControl("cktaisancodinh");
                            p = new SqlParameter[20];
                            p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                            p[1] = new SqlParameter("@NoiDung", txtnoidung.Text.Trim());
                            p[1].SqlDbType = SqlDbType.NVarChar;
                            p[2] = new SqlParameter("@DoiTuong", ex.GetGuid(dldoituong.SelectedValue));
                            p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                            p[3] = new SqlParameter("@DonVi", ex.GetGuid(dldonvi.SelectedValue));
                            p[3].SqlDbType = SqlDbType.UniqueIdentifier;
                            p[4] = new SqlParameter("@HangMuc", ex.GetGuid(dlhangmuc.SelectedValue));
                            p[4].SqlDbType = SqlDbType.UniqueIdentifier;
                            p[5] = new SqlParameter("@SoLuong", double.Parse(txtsoluong.Text.Trim().Equals("") ? "0" : txtsoluong.Text.Trim()));
                            p[5].SqlDbType = SqlDbType.Float;
                            p[6] = new SqlParameter("@DonGia", double.Parse(txtdongia.Text.Trim().Equals("") ? "0" : txtdongia.Text.Trim()));
                            p[6].SqlDbType = SqlDbType.Float;
                            p[7] = new SqlParameter("@TienThanhToan", double.Parse(txtthanhtien.Text.Trim().Equals("") ? "0" : txtthanhtien.Text.Trim()));
                            p[7].SqlDbType = SqlDbType.Float;
                            p[8] = new SqlParameter("@CoVAT", ckVAT.Checked ? 1 : 2);
                            p[8].SqlDbType = SqlDbType.Int;
                            p[9] = new SqlParameter("@CoHoaDon", DBNull.Value);
                            p[9].SqlDbType = SqlDbType.Int;
                            p[10] = new SqlParameter("@IDThanhToan", gID);
                            p[10].SqlDbType = SqlDbType.UniqueIdentifier;
                            p[11] = new SqlParameter("@Del", 1);
                            p[11].SqlDbType = SqlDbType.Int;
                            p[12] = new SqlParameter("@NhaCungCap", ex.GetGuid(dlnhacungcap.SelectedValue));
                            p[12].SqlDbType = SqlDbType.UniqueIdentifier;
                            p[13] = new SqlParameter("@NhomVatLieu", ex.GetGuid(dlnhomvatlieu.SelectedValue));
                            p[13].SqlDbType = SqlDbType.UniqueIdentifier;
                            p[14] = new SqlParameter("@LoaiVatLieu", ex.GetGuid(dlloaivatlieu.SelectedValue));
                            p[14].SqlDbType = SqlDbType.UniqueIdentifier;
                            p[15] = new SqlParameter("@MaThietBi", txtmathietbi.Text.Trim());
                            p[15].SqlDbType = SqlDbType.NVarChar;
                            p[16] = new SqlParameter("@TinhChoChiHa", cktinhchochiha.Checked ? 1 : 2);
                            p[16].SqlDbType = SqlDbType.Int;
                            p[17] = new SqlParameter("@TaiSanCoDinh", cktaisancodinh.Checked ? 1 : 2);
                            p[17].SqlDbType = SqlDbType.Int;
                            p[18] = new SqlParameter("@NhapKho", cknhapkho.Checked ? 1 : 2);
                            p[18].SqlDbType = SqlDbType.Int;
                            p[19] = new SqlParameter("@ThanhTienHang", double.Parse(txtthanhtienhang.Text.Trim().Equals("") ? "0" : txtthanhtienhang.Text.Trim()));
                            p[19].SqlDbType = SqlDbType.Float;
                            ex.ExecuteNonQuerry("sp_XDANoiDungTTTienMat_Add", p);
                        }

                        if (txtsotienton.Text.Equals("0") && drphieuchiton.SelectedIndex == 0)
                        {
                            foreach (System.Web.UI.WebControls.ListItem item in drphieuchi.Items)
                            {
                                if (item.Selected)
                                {
                                    Guid id = new Guid(item.Value);
                                    p = new SqlParameter[1];
                                    p[0] = new SqlParameter("@idlenhchi", id);
                                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                                    dt = ex.GetData("sp_TLPhieuChiTienMat_SoTienTheoPhieuChi", p);
                                    if (dt.Rows.Count > 0)
                                    {
                                        double sotien = double.Parse(dt.Rows[0]["sotien"].ToString());
                                        p = new SqlParameter[5];
                                        p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                                        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[1] = new SqlParameter("@IDThanhToan", gID);
                                        p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[2] = new SqlParameter("@IDPhieuChi", id);
                                        p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                                        p[3] = new SqlParameter("@SoTienThanhToan", sotien);
                                        p[3].SqlDbType = SqlDbType.Float;
                                        p[4] = new SqlParameter("@Del", 1);
                                        p[4].SqlDbType = SqlDbType.Int;
                                        ex.ExecuteNonQuerry("sp_XDAChiTietThanhToan_Add", p);
                                    }
                                }

                            }
                        }
                        else if (double.Parse(txtsotienton.Text) != 0)
                        {
                            foreach (System.Web.UI.WebControls.ListItem item in drphieuchi.Items)
                            {
                                if (item.Selected)
                                {
                                    if (!item.Value.Equals(drphieuchiton.SelectedValue))
                                    {
                                        Guid id = new Guid(item.Value);
                                        p = new SqlParameter[1];
                                        p[0] = new SqlParameter("@idlenhchi", id);
                                        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                                        dt = ex.GetData("sp_TLPhieuChiTienMat_SoTienTheoPhieuChi", p);
                                        if (dt.Rows.Count > 0)
                                        {
                                            double sotien = double.Parse(dt.Rows[0]["sotien"].ToString());
                                            p = new SqlParameter[5];
                                            p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                                            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                                            p[1] = new SqlParameter("@IDThanhToan", gID);
                                            p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                                            p[2] = new SqlParameter("@IDPhieuChi", id);
                                            p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                                            p[3] = new SqlParameter("@SoTienThanhToan", sotien);
                                            p[3].SqlDbType = SqlDbType.Float;
                                            p[4] = new SqlParameter("@Del", 1);
                                            p[4].SqlDbType = SqlDbType.Int;
                                            ex.ExecuteNonQuerry("sp_XDAChiTietThanhToan_Add", p);
                                        }
                                    }
                                    else
                                    {
                                        Guid id = new Guid(item.Value);
                                        p = new SqlParameter[1];
                                        p[0] = new SqlParameter("@idlenhchi", id);
                                        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                                        dt = ex.GetData("sp_TLPhieuChiTienMat_SoTienTheoPhieuChi", p);
                                        if (dt.Rows.Count > 0)
                                        {
                                            double sotien = double.Parse(dt.Rows[0]["sotien"].ToString());
                                            double tienton = double.Parse(txtsotienton.Text.Trim());
                                            p = new SqlParameter[5];
                                            p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                                            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                                            p[1] = new SqlParameter("@IDThanhToan", gID);
                                            p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                                            p[2] = new SqlParameter("@IDPhieuChi", id);
                                            p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                                            p[3] = new SqlParameter("@SoTienThanhToan", sotien - double.Parse(txtsotienton.Text.Trim()));
                                            p[3].SqlDbType = SqlDbType.Float;
                                            p[4] = new SqlParameter("@Del", 1);
                                            p[4].SqlDbType = SqlDbType.Int;
                                            ex.ExecuteNonQuerry("sp_XDAChiTietThanhToan_Add", p);
                                        }
                                    }
                                }

                            }

                        }

                        if (ckhoantheophieuchi.Checked == true)
                        {
                            tienton = double.Parse(txtsotienton.Text.Trim());
                            if (tienton < 0)
                            {
                                p = new SqlParameter[10];
                                p[0] = new SqlParameter("@IDs", gID);
                                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                                p[1] = new SqlParameter("@NhanVien", ex.GetGuid(drnhanvien.SelectedValue));
                                p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                                p[2] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                                p[2].SqlDbType = SqlDbType.SmallDateTime;
                                p[3] = new SqlParameter("@SoTien", double.Parse(txtsotienhoanlai.Text.Trim().Equals("") ? "0" : txtsotienhoanlai.Text.Trim()));
                                p[3].SqlDbType = SqlDbType.Float;
                                p[4] = new SqlParameter("@NoiDung", DBNull.Value);
                                p[4].SqlDbType = SqlDbType.NVarChar;

                                p[5] = new SqlParameter("@NgayTao", DateTime.Now);
                                p[5].SqlDbType = SqlDbType.SmallDateTime;
                                p[6] = new SqlParameter("@NguoiTao", Session["user"].ToString());
                                p[6].SqlDbType = SqlDbType.NVarChar;
                                p[7] = new SqlParameter("@Del", 1);
                                p[7].SqlDbType = SqlDbType.Int;
                                p[8] = new SqlParameter("@UngLuong", DBNull.Value);
                                p[8].SqlDbType = SqlDbType.Int;
                                p[9] = new SqlParameter("@TinhChoChiHa", DBNull.Value);
                                p[9].SqlDbType = SqlDbType.Int;
                                ex.ExecuteNonQuerry("sp_xdathietlapphieuchitienmat_Update_ThuongLuong", p);
                                p = new SqlParameter[3];
                                p[0] = new SqlParameter("@IDs", gID);
                                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                                p[1] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                                p[1].SqlDbType = SqlDbType.SmallDateTime;
                                p[2] = new SqlParameter("@SoTien", double.Parse(txtsotien.Text.Trim().Equals("") ? "0" : txtsotien.Text.Trim()));
                                p[2].SqlDbType = SqlDbType.Float;
                                ex.ExecuteNonQuerry("sp_ThietLapPhieuChiTienMat_UpDate_PCCongNo_ThuongLUong", p);
                                p = new SqlParameter[2];
                                p[0] = new SqlParameter("@IDs", gID);
                                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                                p[1] = new SqlParameter("@NoiDung", "Thanh toán bù cho phiếu chi " + drphieuchiton.SelectedItem.Text);
                                p[1].SqlDbType = SqlDbType.NVarChar;
                                ex.ExecuteNonQuerry("sp_ThietLapPhieuChiTienMat_UpDate_MoiDungPC_ThuongLUong", p);

                            }
                            else
                            {
                                p = new SqlParameter[8];
                                p[0] = new SqlParameter("@IDs", gID);
                                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                                p[1] = new SqlParameter("@NhanVien", ex.GetGuid(drnhanvien.SelectedValue));
                                p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                                p[2] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                                p[2].SqlDbType = SqlDbType.SmallDateTime;
                                p[3] = new SqlParameter("@SoTien", double.Parse(txtsotienhoanlai.Text.Trim().Equals("") ? "0" : txtsotienhoanlai.Text.Trim()));
                                p[3].SqlDbType = SqlDbType.Float;
                                p[4] = new SqlParameter("@NoiDung", "Thanh toán bù cho phiếu chi " + drphieuchiton.SelectedItem.Text);
                                p[4].SqlDbType = SqlDbType.NVarChar;

                                p[5] = new SqlParameter("@NgayTao", DateTime.Now);
                                p[5].SqlDbType = SqlDbType.SmallDateTime;
                                p[6] = new SqlParameter("@NguoiTao", Session["user"].ToString());
                                p[6].SqlDbType = SqlDbType.NVarChar;
                                p[7] = new SqlParameter("@Del", 1);
                                p[7].SqlDbType = SqlDbType.Int;

                                ex.ExecuteNonQuerry("sp_xdathietlapphieuthutienmat_Update_ThuongLuong", p);
                                p = new SqlParameter[3];
                                p[0] = new SqlParameter("@IDs", gID);
                                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                                p[1] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                                p[1].SqlDbType = SqlDbType.SmallDateTime;
                                p[2] = new SqlParameter("@SoTien", double.Parse(txtsotienhoanlai.Text.Trim().Equals("") ? "0" : txtsotienhoanlai.Text.Trim()));
                                p[2].SqlDbType = SqlDbType.Float;
                                ex.ExecuteNonQuerry("sp_ThietLapPhieuthuTienMat_UpDate_PCCongNo_ThuongLUong", p);
                                p = new SqlParameter[2];
                                p[0] = new SqlParameter("@IDs", gID);
                                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                                p[1] = new SqlParameter("@NoiDung", "Thanh toán bù cho phiếu chi " + drphieuchiton.SelectedItem.Text);
                                p[1].SqlDbType = SqlDbType.NVarChar;
                                ex.ExecuteNonQuerry("sp_ThietLapPhieuthuTienMat_UpDate_MoiDungPC_ThuongLUong", p);
                            }
                        }
                        for (int i = 0; i < GV1.Rows.Count; i++)
                        {
                            Guid phieuchi = Guid.NewGuid();
                            Guid idtt = Guid.NewGuid();
                            Guid nhapkho = Guid.NewGuid();
                            TextBox txtnoidung = (TextBox)GV1.Rows[i].Cells[1].FindControl("txtnoidung");
                            DropDownList dlhangmuc = (DropDownList)GV1.Rows[i].Cells[2].FindControl("dlhangmuc");
                            DropDownList dldoituong = (DropDownList)GV1.Rows[i].Cells[3].FindControl("dldoituong");
                            DropDownList dlnhacungcap = (DropDownList)GV1.Rows[i].Cells[4].FindControl("drnhacungcap");
                            DropDownList dlnhomvatlieu = (DropDownList)GV1.Rows[i].Cells[5].FindControl("drnhomvatlieu");
                            DropDownList dlloaivatlieu = (DropDownList)GV1.Rows[i].Cells[6].FindControl("drloaivatlieu");
                            DropDownList dldonvi = (DropDownList)GV1.Rows[i].Cells[7].FindControl("dldonvi");
                            TextBox txtsoluong = (TextBox)GV1.Rows[i].Cells[8].FindControl("txtsoluong");
                            TextBox txtdongia = (TextBox)GV1.Rows[i].Cells[9].FindControl("txtdongia");
                            TextBox txtthanhtienhang = (TextBox)GV1.Rows[i].Cells[10].FindControl("txtthanhtienhang");
                            TextBox txtthanhtien = (TextBox)GV1.Rows[i].Cells[11].FindControl("txtthanhtien");
                            TextBox txtmathietbi = (TextBox)GV1.Rows[i].Cells[12].FindControl("txtmathietbi");
                            CheckBox ckVAT = (CheckBox)GV1.Rows[i].Cells[13].FindControl("ckVAT");

                            CheckBox cknhapkho = (CheckBox)GV1.Rows[i].Cells[14].FindControl("cknhapkho");
                            if (cknhapkho.Checked == true)
                            {
                                if (dlnhacungcap.SelectedIndex == 0 || dlnhomvatlieu.SelectedIndex == 0 || dlloaivatlieu.SelectedIndex == 0)
                                {

                                    gstGetMess("Chọn nhà cung cấp,nhóm vật liệu,loại vật liệu", "");
                                }
                                else
                                {
                                    # region insert nhapkho
                                    p = new SqlParameter[15];
                                    p[0] = new SqlParameter("@IDs", nhapkho);
                                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                                    p[1] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                                    p[1].SqlDbType = SqlDbType.SmallDateTime;
                                    p[2] = new SqlParameter("@NhaCungCap", ex.GetGuid(dlnhacungcap.SelectedValue));
                                    p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                                    p[3] = new SqlParameter("@NguoiMua", ex.GetGuid(drnhanvien.SelectedValue));
                                    p[3].SqlDbType = SqlDbType.UniqueIdentifier;
                                    p[4] = new SqlParameter("@NhomVatLieu", ex.GetGuid(dlnhomvatlieu.SelectedValue));
                                    p[4].SqlDbType = SqlDbType.UniqueIdentifier;
                                    p[5] = new SqlParameter("@LoaiVatLieu", ex.GetGuid(dlloaivatlieu.SelectedValue));
                                    p[5].SqlDbType = SqlDbType.UniqueIdentifier;
                                    p[6] = new SqlParameter("@DonVi", ex.GetGuid(dldonvi.SelectedValue));
                                    p[6].SqlDbType = SqlDbType.UniqueIdentifier;
                                    p[7] = new SqlParameter("@SoLuong", double.Parse(txtsoluong.Text.Trim().Equals("") ? "0" : txtsoluong.Text.Trim()));
                                    p[7].SqlDbType = SqlDbType.Float;
                                    p[8] = new SqlParameter("@DonGia", double.Parse(txtdongia.Text.Trim().Equals("") ? "0" : txtdongia.Text.Trim()));
                                    p[8].SqlDbType = SqlDbType.Float;
                                    p[9] = new SqlParameter("@ThanhTien", double.Parse(txtthanhtien.Text.Trim().Equals("") ? "0" : txtthanhtien.Text.Trim()));
                                    p[9].SqlDbType = SqlDbType.Float;
                                    p[10] = new SqlParameter("@MaThietBi", txtmathietbi.Text.Trim());
                                    p[10].SqlDbType = SqlDbType.NVarChar;
                                    p[11] = new SqlParameter("@Del", 1);
                                    p[11].SqlDbType = SqlDbType.Int;
                                    p[12] = new SqlParameter("@CoVAT", ckVAT.Checked ? 1 : 2);
                                    p[12].SqlDbType = SqlDbType.Int;
                                    p[13] = new SqlParameter("@IDThanhToan", gID);
                                    p[13].SqlDbType = SqlDbType.UniqueIdentifier;
                                    p[14] = new SqlParameter("@ThanhTienHang", double.Parse(txtthanhtienhang.Text.Trim().Equals("") ? "0" : txtthanhtienhang.Text.Trim()));
                                    p[14].SqlDbType = SqlDbType.Float;
                                    ex.ExecuteNonQuerry("sp_xdanhapkho_Add", p);
                                    #endregion
                                    #region


                                    p = new SqlParameter[1];
                                    p[0] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                                    p[0].SqlDbType = SqlDbType.SmallDateTime;
                                    dt = ex.GetData("sp_xdagetsophieuchiconnotravat_TienMat", p);
                                    int sopc = Int16.Parse(dt.Rows[0]["sopc"].ToString());
                                    if (sopc == 0)
                                        pc = 1;
                                    else pc = sopc + 1;

                                    p = new SqlParameter[21];
                                    p[0] = new SqlParameter("@IDs", idtt);
                                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                                    p[1] = new SqlParameter("@NhaCungCap", ex.GetGuid(dlnhacungcap.SelectedValue));
                                    p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                                    p[2] = new SqlParameter("@SoDuKiTruocVAT", double.Parse(0.ToString()));
                                    p[2].SqlDbType = SqlDbType.Float;

                                    p[3] = new SqlParameter("@TienCuocVAT", double.Parse(0.ToString()));
                                    p[3].SqlDbType = SqlDbType.Float;
                                    p[4] = new SqlParameter("@SoDuKiTruocKhongVAT", double.Parse(0.ToString()));
                                    p[4].SqlDbType = SqlDbType.Float;

                                    p[5] = new SqlParameter("@TienCuocKhongVAT", double.Parse(0.ToString()));
                                    p[5].SqlDbType = SqlDbType.Float;
                                    p[6] = new SqlParameter("@SoTienThanhToanTruocVAT", double.Parse(0.ToString()));
                                    p[6].SqlDbType = SqlDbType.Float;
                                    p[7] = new SqlParameter("@SoTienThanhToanKhongVAT", double.Parse(0.ToString()));
                                    p[7].SqlDbType = SqlDbType.Float;
                                    if (ckVAT.Checked == true)
                                    {
                                        p[8] = new SqlParameter("@TienHangVAT", double.Parse(txtthanhtienhang.Text.Trim()));
                                        p[8].SqlDbType = SqlDbType.Float;
                                        p[9] = new SqlParameter("@TienHangKhongVAT", double.Parse(0.ToString()));
                                        p[9].SqlDbType = SqlDbType.Float;
                                        p[10] = new SqlParameter("@SoTienThanhToanKiNayVAT", double.Parse(txtthanhtien.Text.Trim().Equals("") ? "0" : txtthanhtien.Text.Trim()));
                                        p[10].SqlDbType = SqlDbType.Float;
                                        p[11] = new SqlParameter("@SoTienThanhToanKiNayKhongVAT", double.Parse(0.ToString()));
                                        p[11].SqlDbType = SqlDbType.Float;
                                    }
                                    else
                                    {
                                        p[8] = new SqlParameter("@TienHangVAT", double.Parse(0.ToString()));
                                        p[8].SqlDbType = SqlDbType.Float;
                                        p[9] = new SqlParameter("@TienHangKhongVAT", double.Parse(txtthanhtienhang.Text.Trim()));
                                        p[9].SqlDbType = SqlDbType.Float;
                                        p[10] = new SqlParameter("@SoTienThanhToanKiNayVAT", double.Parse(0.ToString()));
                                        p[10].SqlDbType = SqlDbType.Float;
                                        p[11] = new SqlParameter("@SoTienThanhToanKiNayKhongVAT", double.Parse(txtthanhtien.Text.Trim().Equals("") ? "0" : txtthanhtien.Text.Trim()));
                                        p[11].SqlDbType = SqlDbType.Float;
                                    }
                                    p[12] = new SqlParameter("@HinhThucThanhToanVAT", 2);
                                    p[12].SqlDbType = SqlDbType.Int;
                                    p[13] = new SqlParameter("@HinhThucThanhToanKhongVAT", double.Parse(0.ToString()));
                                    p[13].SqlDbType = SqlDbType.Int;
                                    p[14] = new SqlParameter("@SoPC", pc);
                                    p[14].SqlDbType = SqlDbType.Int;
                                    p[15] = new SqlParameter("@TaiKhoanKhongVAT", DBNull.Value);
                                    p[15].SqlDbType = SqlDbType.UniqueIdentifier;
                                    p[16] = new SqlParameter("@TaiKhoanVAT", DBNull.Value);
                                    p[16].SqlDbType = SqlDbType.UniqueIdentifier;
                                    p[17] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                                    p[17].SqlDbType = SqlDbType.SmallDateTime;
                                    p[18] = new SqlParameter("@Del", 1);
                                    p[18].SqlDbType = SqlDbType.Int;
                                    p[19] = new SqlParameter("@NoiDung", txtnoidung.Text.Trim());
                                    p[19].SqlDbType = SqlDbType.NVarChar;
                                    p[20] = new SqlParameter("@IDLenhChi", gID);
                                    p[20].SqlDbType = SqlDbType.UniqueIdentifier;
                                    ex.ExecuteNonQuerry("sp_XDAThanhToanCongNoTra_Add", p);

                                    p = new SqlParameter[11];
                                    p[0] = new SqlParameter("@IDs", phieuchi);
                                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                                    p[1] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                                    p[1].SqlDbType = SqlDbType.SmallDateTime;
                                    p[2] = new SqlParameter("@NoiDung", txtnoidung.Text.Trim());
                                    p[2].SqlDbType = SqlDbType.NVarChar;
                                    p[3] = new SqlParameter("@DonVi", ex.GetGuid(dlnhacungcap.SelectedValue));
                                    p[3].SqlDbType = SqlDbType.UniqueIdentifier;
                                    p[4] = new SqlParameter("@SoPC", pc);
                                    p[4].SqlDbType = SqlDbType.Int;
                                    p[5] = new SqlParameter("@TaiKhoan", DBNull.Value);
                                    p[5].SqlDbType = SqlDbType.UniqueIdentifier;
                                    p[6] = new SqlParameter("@Del", 1);
                                    p[6].SqlDbType = SqlDbType.Int;
                                    p[7] = new SqlParameter("@LoaiCongNo", 2);
                                    p[7].SqlDbType = SqlDbType.Int;
                                    p[8] = new SqlParameter("@SoTien", double.Parse(txtthanhtien.Text.Trim().Equals("") ? "0" : txtthanhtien.Text.Trim()));
                                    p[8].SqlDbType = SqlDbType.Float;
                                    p[9] = new SqlParameter("@IDLenhChi", idtt);
                                    p[9].SqlDbType = SqlDbType.UniqueIdentifier;
                                    p[10] = new SqlParameter("@NhanVien", ex.GetGuid(drnhanvien.SelectedValue));
                                    p[10].SqlDbType = SqlDbType.UniqueIdentifier;
                                    ex.ExecuteNonQuerry("sp_xdaphieuchicongno_Add", p);

                                    #endregion}
                                }
                            }
                        }

                        btnCancel_Click(sender, e);
                        gstGetMess("Cập nhật thành công", "");
                        btnSave.Visible = true;
                        btncapnhat.Visible = false;
                    }
            }
            catch (Exception ax)
            {
                gstGetMess("Có lỗi trong quá trình lưu, xem trong file log", "");
                ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tThiết lập thanh toán tiền mặt\t" + ax.Message);
            }
        }
        else
            gstGetMess("Bạn không có quyền thêm dữ liệu trên trang này", "");
    }

    protected void Button4_Click(object sender, EventArgs e)
    {
        CurrentPage = 1;
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", CurrentPage);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;

        GVVAT.DataSource = ex.GetData("sp_xdanoidungtttienmat_VAT", p);
        GVVAT.DataBind();

        Button5.Enabled = false;
        Button6.Enabled = true;
        Button4.Enabled = false;
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        if (CurrentPage > 1)
        {
            CurrentPage--;
            Button5.Enabled = true;
            Button6.Enabled = true;
            Button4.Enabled = true;

            p = new SqlParameter[2];
            p[0] = new SqlParameter("@PageNumber", CurrentPage);
            p[0].SqlDbType = SqlDbType.Int;
            p[1] = new SqlParameter("@RowspPage", PageNumber);
            p[1].SqlDbType = SqlDbType.Int;

            GVVAT.DataSource = ex.GetData("sp_xdanoidungtttienmat_VAT", p);
            GVVAT.DataBind();

        }
        else
        {
            Button4.Enabled = false;
            Button5.Enabled = false;
            Button6.Enabled = true;
        }
    }
    protected void Button6_Click(object sender, EventArgs e)
    {
        CurrentPage++;
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", CurrentPage);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;

        GVVAT.DataSource = ex.GetData("sp_xdanoidungtttienmat_VAT", p);
        GVVAT.DataBind();

        if (GV.Rows.Count.Equals(0))
        {
            Button5.Enabled = true;
            Button6.Enabled = false;
        }
        else
        {
            Button5.Enabled = true;
            Button6.Enabled = true;
        }
        Button4.Enabled = true;
    }
    protected void btnxemvat_Click(object sender, EventArgs e)
    {
        getDate();

        p = new SqlParameter[2];
        p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
        p[0].SqlDbType = SqlDbType.SmallDateTime;
        p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
        p[1].SqlDbType = SqlDbType.SmallDateTime;
        GVVAT.DataSource = ex.GetData("sp_xdanoidungtttienmat_VAT_TheoThoiGian", p);
        GVVAT.DataBind();
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
        p[0].SqlDbType = SqlDbType.SmallDateTime;
        p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
        p[1].SqlDbType = SqlDbType.SmallDateTime;
        dt = ex.GetData("sp_xdanoidungtttienmat_VAT_TheoThoiGian", p);
        LoadFooter2();
        Button4.Visible = false;
        Button5.Visible = false; Button6.Visible = false;
    }
    protected void cktinhchochiha_CheckedChanged(object sender, EventArgs e)
    {
        ListPhieuChiTon_TinhChoChiHa();
        ListPhieuChiTheoNhanVien_TinhChoChiHa();
    }
    protected void Button9_Click(object sender, EventArgs e)
    {
        PrintNPOI(dtPrint);
    }

    void PrintNPOI(System.Data.DataTable dt)
    {
        var workbook = new HSSFWorkbook();

        if (dt.Rows.Count > 0)
        {
            string sheetname = "Danh sách";




            var sheet = workbook.CreateSheet(sheetname);

            #region CSS
            var headerLabelCellStyle = workbook.CreateCellStyle();
            headerLabelCellStyle.Alignment = HorizontalAlignment.LEFT;
            headerLabelCellStyle.BorderBottom = CellBorderType.THIN;
            headerLabelCellStyle.BorderLeft = CellBorderType.THIN;
            headerLabelCellStyle.BorderRight = CellBorderType.THIN;
            headerLabelCellStyle.BorderTop = CellBorderType.THIN;

            var headerLabelFont = workbook.CreateFont();
            headerLabelFont.FontName = "Times New Roman";

            headerLabelFont.FontHeightInPoints = 11;
            headerLabelFont.Color = HSSFColor.BLACK.index;
            headerLabelCellStyle.SetFont(headerLabelFont);

            //--------------------------

            var hsRight = workbook.CreateCellStyle();
            hsRight.Alignment = HorizontalAlignment.RIGHT;
            hsRight.BorderBottom = CellBorderType.THIN;
            hsRight.BorderLeft = CellBorderType.THIN;
            hsRight.BorderRight = CellBorderType.THIN;
            hsRight.BorderTop = CellBorderType.THIN;

            hsRight.SetFont(headerLabelFont);
            //-------------------
            var hsCenter = workbook.CreateCellStyle();
            hsCenter.Alignment = HorizontalAlignment.CENTER;
            hsCenter.BorderBottom = CellBorderType.THIN;
            hsCenter.BorderLeft = CellBorderType.THIN;
            hsCenter.BorderRight = CellBorderType.THIN;
            hsCenter.BorderTop = CellBorderType.THIN;

            hsCenter.SetFont(headerLabelFont);
            //-----------------------

            var hs1 = workbook.CreateCellStyle();
            hs1.Alignment = HorizontalAlignment.LEFT;

            var hsb = workbook.CreateFont();
            hsb.Boldweight = (short)FontBoldWeight.BOLD;
            hsb.FontName = "Times New Roman";

            hsb.FontHeightInPoints = 11;
            hsb.Color = HSSFColor.BLACK.index;
            hs1.SetFont(hsb);

            //-------------
            var hs2 = workbook.CreateCellStyle();
            hs2.Alignment = HorizontalAlignment.CENTER;

            var hsb2 = workbook.CreateFont();
            hsb2.Boldweight = (short)FontBoldWeight.BOLD;
            hsb2.FontName = "Times New Roman";

            hsb2.FontHeightInPoints = 18;
            hsb2.Color = HSSFColor.BLACK.index;
            hs2.SetFont(hsb2);
            //------------------------------
            var hs3 = workbook.CreateCellStyle();
            hs3.Alignment = HorizontalAlignment.CENTER;
            hs3.BorderBottom = CellBorderType.THIN;
            var hsb3 = workbook.CreateFont();
            hsb3.Boldweight = (short)FontBoldWeight.BOLD;
            hsb3.FontName = "Times New Roman";

            hsb3.FontHeightInPoints = 14;
            hsb3.Color = HSSFColor.BLACK.index;
            hs3.SetFont(hsb3);

            //----------------------------
            var hs4 = workbook.CreateCellStyle();
            hs4.Alignment = HorizontalAlignment.CENTER;

            hs4.BorderBottom = CellBorderType.THIN;
            hs4.BorderLeft = CellBorderType.THIN;
            hs4.BorderRight = CellBorderType.THIN;
            hs4.BorderTop = CellBorderType.THIN;

            var hsb4 = workbook.CreateFont();
            hsb4.Boldweight = (short)FontBoldWeight.BOLD;
            hsb4.FontName = "Times New Roman";

            hsb4.FontHeightInPoints = 11;
            hsb4.Color = HSSFColor.BLACK.index;
            hs4.SetFont(hsb4);

            #endregion

            //Start header-----------------------------------------------------------------------
            #region Header
            //tao hnag dau tien
            var rowIndex = 0;

            //cong ty
            var row = sheet.CreateRow(rowIndex);
            Cell r1c1 = row.CreateCell(0);
            r1c1.SetCellValue("Công ty cổ phần Xây Dựng Đức Anh");
            r1c1.CellStyle = hs1;
            r1c1.Row.Height = 400;

            CellRangeAddress cra = new CellRangeAddress(0, 0, 0, 4);
            sheet.AddMergedRegion(cra);

            //chi nhanh
            rowIndex++;
            row = sheet.CreateRow(rowIndex);
            r1c1 = row.CreateCell(0);
            r1c1.SetCellValue("Chi nhánh Trạm trộn Lâm Thao");
            r1c1.CellStyle = hs1;
            r1c1.Row.Height = 400;

            cra = new CellRangeAddress(1, 1, 0, 4);
            sheet.AddMergedRegion(cra);

            //tieu de bao cao
            string ngaythang = dlThang.SelectedIndex > 0 ? dlThang.SelectedItem.Text + " " + dlNam.SelectedItem.Text : "Từ " + txtTuNgay.Text + " Đến " + txtDenNgay.Text;
            //  string congtrinh = dlCongTrinh.SelectedIndex > 0 ? " - " + dlCongTrinh.SelectedItem.Text : "";
            rowIndex++;
            row = sheet.CreateRow(rowIndex);
            r1c1 = row.CreateCell(0);
            r1c1.SetCellValue("Danh sách thanh toán tiền mặt " + ngaythang);
            r1c1.CellStyle = hs2;
            r1c1.Row.Height = 500;

            cra = new CellRangeAddress(2, 2, 0, 12);
            sheet.AddMergedRegion(cra);

            //khachhang
            //rowIndex++;
            //row = sheet.CreateRow(rowIndex);
            //r1c1 = row.CreateCell(0);
            //r1c1.SetCellValue(dlKhachHang.SelectedIndex > 0 ? "Khách hàng: " + dlKhachHang.SelectedItem.Text + congtrinh : dlLoaiKH.SelectedIndex > 0 ? "Loại khách hàng: " + dlLoaiKH.SelectedItem.Text : "");
            //r1c1.CellStyle = hs3;
            //r1c1.Row.Height = 500;



            //freeze panes

            #endregion
            //End header-----------------------------------------------------------------------

            #region Header1-2

            //header2
            rowIndex++;
            rowIndex++;
            row = sheet.CreateRow(rowIndex);
            string[] header = { "Ngày tháng", "Họ tên", "Số phiếu TT", "Số PC", "Số tiền TT", "Số tiền còn lại", "Số PT","Số hoàn chi"
                              };

            var cell = row.CreateCell(0);
            cell.SetCellValue("STT");
            cell.CellStyle = hs4;
            cell.Row.Height = 400;


            //tao tieu de cot
            for (int j = 0; j < header.Length; j++)
            {
                cell = row.CreateCell(j + 1);
                cell.SetCellValue(header[j].ToString());
                cell.CellStyle = hs4;
                cell.Row.Height = 500;

                sheet.AutoSizeColumn(j);

            }

            #endregion

            //ghi du lieu tung dong
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                rowIndex++;
                row = sheet.CreateRow(rowIndex);
                cell = row.CreateCell(0);
                cell.SetCellValue((i + 1).ToString());
                cell.CellStyle = hsCenter;

                for (int j = 1; j <= dt.Columns.Count; j++)
                {
                    cell = row.CreateCell(j);

                    if (j == 5 || j == 6)
                    {
                        if (!dt.Rows[i][j - 1].ToString().Trim().Equals(""))
                        {
                            cell.SetCellType(CellType.NUMERIC);
                            cell.SetCellValue(double.Parse(dt.Rows[i][j - 1].ToString()));
                        }
                        else
                            cell.SetCellValue(dt.Rows[i][j - 1].ToString());
                        cell.CellStyle = hsRight;
                    }
                    else
                    {
                        cell.SetCellValue(dt.Rows[i][j - 1].ToString());

                        if (j <= 1)
                            cell.CellStyle = hsCenter;
                        else
                            cell.CellStyle = headerLabelCellStyle;
                    }
                    cell.Row.Height = 500;
                }

            }


            rowIndex++;
            row = sheet.CreateRow(rowIndex);

            cell = row.CreateCell(2);
            cell.SetCellValue("Tổng cộng");
            cell.Row.Height = 500;
            cell.CellStyle = hs4;

            string[] footer = { "F", "G" };
            for (int k = 5; k <= 6; k++)
            {

                cell = row.CreateCell(k);
                cell.CellFormula = "Sum(" + footer[k - 5] + "6:" + footer[k - 5] + rowIndex.ToString() + ")";
                cell.Row.Height = 500;
                cell.CellStyle = hs4;
            }
            //}
            //fix lai border
            r1c1 = row.CreateCell(0);
            r1c1.CellStyle = hs4;
            r1c1.Row.Height = 400;

            r1c1 = row.CreateCell(1);
            r1c1.CellStyle = hs4;
            r1c1.Row.Height = 400;
            r1c1 = row.CreateCell(3);
            r1c1.CellStyle = hs4;
            r1c1.Row.Height = 400;

            r1c1 = row.CreateCell(4);
            r1c1.CellStyle = hs4;
            r1c1.Row.Height = 400;

            r1c1 = row.CreateCell(7);
            r1c1.CellStyle = hs4;
            r1c1.Row.Height = 400;

            r1c1 = row.CreateCell(8);
            r1c1.CellStyle = hs4;
            r1c1.Row.Height = 400;
            for (int ik = 0; ik <= 8; ik++)
            {
                sheet.AutoSizeColumn(ik);
            }


            //end sheet tong hop--------------------------
            using (var exportData = new MemoryStream())
            {
                workbook.Write(exportData);

                string saveAsFileName = "DSTTThanhtoantienmat-" + " " + ngaythang + ".xls";

                Response.ContentType = "application/vnd.ms-excel";
                Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", saveAsFileName));
                Response.Clear();
                Response.BinaryWrite(exportData.GetBuffer());
                Response.End();
            }
        }
    }
    public string ToString(decimal number)
    {
        string s = number.ToString("#");
        string[] so = new string[] { "không", "một", "hai", "ba", "bốn", "năm", "sáu", "bảy", "tám", "chín" };
        string[] hang = new string[] { "", "nghìn", "triệu", "tỷ" };
        int i, j, donvi, chuc, tram;
        string str = " ";
        bool booAm = false;
        decimal decS = 0;
        //Tung addnew
        try
        {
            decS = Convert.ToDecimal(s.ToString());
        }
        catch
        {
            return "";
        }
        if (decS < 0)
        {
            decS = -decS;
            s = decS.ToString();
            booAm = true;
        }
        i = s.Length;
        if (i == 0)
            str = so[0] + str;
        else
        {
            j = 0;
            while (i > 0)
            {
                donvi = Convert.ToInt32(s.Substring(i - 1, 1));
                i--;
                if (i > 0)
                    chuc = Convert.ToInt32(s.Substring(i - 1, 1));
                else
                    chuc = -1;
                i--;
                if (i > 0)
                    tram = Convert.ToInt32(s.Substring(i - 1, 1));
                else
                    tram = -1;
                i--;
                if ((donvi > 0) || (chuc > 0) || (tram > 0) || (j == 3))
                    str = hang[j] + str;
                j++;
                if (j > 3) j = 1;
                if ((donvi == 1) && (chuc > 1))
                    str = "một " + str;
                else
                {
                    if ((donvi == 5) && (chuc > 0))
                        str = "lăm " + str;
                    else if (donvi > 0)
                        str = so[donvi] + " " + str;
                }
                if (chuc < 0)
                    break;
                else
                {
                    if ((chuc == 0) && (donvi > 0)) str = "lẻ " + str;
                    if (chuc == 1) str = "mười " + str;
                    if (chuc > 1) str = so[chuc] + " mươi " + str;
                }
                if (tram < 0) break;
                else
                {
                    if ((tram > 0) || (chuc > 0) || (donvi > 0)) str = so[tram] + " trăm " + str;
                }
                str = " " + str;
            }
        }
        if (booAm) str = "Âm " + str;
        return str + "đồng chẵn";
    }

    protected void GV_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            GridView HeaderGrid = (GridView)sender;
            GridViewRow HeaderGridRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
            HeaderGridRow.Height = 20;
            HeaderGridRow.HorizontalAlign = HorizontalAlign.Center;
            TableCell HeaderCell = new TableCell();

            HeaderGridRow.Cells.Add(HeaderCell);
            HeaderCell = new TableCell();
            HeaderCell.Text = "";
            HeaderCell.ColumnSpan = 1;

            HeaderGridRow.Cells.Add(HeaderCell);
            HeaderCell = new TableCell();
            HeaderCell.Text = "";
            HeaderCell.ColumnSpan = 1;
            HeaderGridRow.Cells.Add(HeaderCell);
            HeaderCell = new TableCell();
            HeaderCell.Text = "";
            HeaderCell.ColumnSpan = 1;
            HeaderGridRow.Cells.Add(HeaderCell);

            HeaderCell = new TableCell();
            HeaderCell.Text = "";
            HeaderCell.ColumnSpan = 1;
            HeaderGridRow.Cells.Add(HeaderCell);
            HeaderCell = new TableCell();
            HeaderCell.Text = "";
            HeaderCell.ColumnSpan = 1;
            HeaderGridRow.Cells.Add(HeaderCell);


            HeaderCell = new TableCell();
            HeaderCell.Text = "";
            HeaderCell.ColumnSpan = 1;
            HeaderGridRow.Cells.Add(HeaderCell);



            HeaderCell = new TableCell();
            HeaderCell.Text = "Thành tiền";
            HeaderCell.ColumnSpan = 2;
            HeaderGridRow.Cells.Add(HeaderCell);



            GridView2.Controls[0].Controls.AddAt(0, HeaderGridRow);
        }

        if (e.Row.RowType == DataControlRowType.Footer)
        {
           
            e.Row.Cells[7].ColumnSpan = 2;
            e.Row.Cells.RemoveAt(8);
           
                   }
    }

    private void LoadFooter()
    {
        string[] field = { "sotienthanhtoan", "sotienconlai" };
        if (dt.Rows.Count > 0)
        {
            GV.FooterRow.Cells[2].Text = "Tổng cộng";
            GV.FooterRow.Cells[2].HorizontalAlign = HorizontalAlign.Center;
            for (int k = 6; k < 8; k++)
            {
                GV.FooterRow.Cells[k].Text = ConvertDecimal(double.Parse(dt.Compute("sum(" + field[k - 6] + ")", "").ToString()));
                GV.FooterRow.Cells[k].HorizontalAlign = HorizontalAlign.Right;
            }


        }
    }
    private void LoadFooter1()
    {
        string[] field = { "thanhtien" };
        if (dt.Rows.Count > 0)
        {
            GVDoiTuong.FooterRow.Cells[2].Text = "Tổng cộng";
            GVDoiTuong.FooterRow.Cells[2].HorizontalAlign = HorizontalAlign.Center;
            for (int k = 10; k < 11; k++)
            {
                GVDoiTuong.FooterRow.Cells[k].Text = ConvertDecimal(double.Parse(dt.Compute("sum(" + field[k - 10] + ")", "").ToString()));
                GVDoiTuong.FooterRow.Cells[k].HorizontalAlign = HorizontalAlign.Right;
            }


        }
    }

    private void LoadFooter2()
    {
        string[] field = { "thanhtien" };
        if (dt.Rows.Count > 0)
        {
            GVVAT.FooterRow.Cells[2].Text = "Tổng cộng";
            GVVAT.FooterRow.Cells[2].HorizontalAlign = HorizontalAlign.Center;
            for (int k = 10; k < 11; k++)
            {
                GVVAT.FooterRow.Cells[k].Text = ConvertDecimal(double.Parse(dt.Compute("sum(" + field[k - 10] + ")", "").ToString()));
                GVVAT.FooterRow.Cells[k].HorizontalAlign = HorizontalAlign.Right;
            }


        }
    }
}